Haskell Multi-line Lambdas

2019-07-07 16:52发布

I am learning Haskell from learnyouahaskell.com and there is an example such that:

search :: (Eq a) => [a] -> [a] -> Bool  
search needle haystack =   
    let nlen = length needle  
    in  foldl (\acc x -
> if take nlen x == needle then True else acc) False (tails haystack) 

But when tried this code with GHC, it gives me

error: parse error on input ‘-’

But it works when it is like this:

search :: (Eq a) => [a] -> [a] -> Bool  
search needle haystack =   
    let nlen = length needle  
    in  foldl (\acc x -> if take nlen x == needle then True else acc) False (tails haystack) 

Is there a feature of Haskell that allows multi-line lambdas or is that something I am missing?

1条回答
爷、活的狠高调
2楼-- · 2019-07-07 17:37

Don't break the ->

Just do:

search :: (Eq a) => [a] -> [a] -> Bool  
search needle haystack =   
   let nlen = length needle  
   in  foldl (\acc x ->
if take nlen x == needle then True else acc) False (tails haystack) 

or

search :: (Eq a) => [a] -> [a] -> Bool  
search needle haystack =   
   let nlen = length needle  
   in  foldl (\acc x 
-> if take nlen x == needle then True else acc) False (tails haystack) 
查看更多
登录 后发表回答