Haskell Multi-line Lambdas

一世执手 提交于 2019-12-24 03:26:28

问题


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:


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) 


来源:https://stackoverflow.com/questions/38673518/haskell-multi-line-lambdas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!