Haskell多行Lambda [英] Haskell Multi-line Lambdas
本文介绍了Haskell多行Lambda的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在从learningyouahaskell.com学习Haskell,有这样的示例:
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)
但是当在GHC上尝试使用此代码时,它给了我
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)
Haskell是否具有允许多行lambda的功能,还是我缺少的东西?
Is there a feature of Haskell that allows multi-line lambdas or is that something I am missing?
推荐答案
不要破坏->
只需:
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)
或
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)
这篇关于Haskell多行Lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文