我想围绕Haskell的语法包扎头脑。守卫和匿名函数列表
这个问题很简单,逻辑解决。我必须分解一个正整数和负整数的列表,并将它们分组,使得
[1,2,3,-1,-2,-3,1,2,3]变成[[1,2, 3],[ - 1,-2,-3],[1,2,3]]
我想使用更高阶的函数,foldr可以用一个匿名函数arguements。
这是我到目前为止。
split = foldr (\ x y -> if (x > 0)
then if (head (head y)) < 0
then [x] : y
else x : head y --error here
else if (x < 0)
then if (head (head y)) > 0
then [x] : y
else x : head y
else y
)
[[]]
这是错误我得到
Occurs check: cannot construct the infinite type: a0 = [a0]
In the first argument of `(:)', namely `x'
In the expression: x : head y
In the expression:
if (head (head y)) < 0 then [x] : y else x : head y
我有两个问题。
1)为什么我在第7行出现类型错误?
我这不是连接来整数列表(头Y)
2)你怎么写出来的使用警卫下述条件的整数(X)?我试图做到这一点,但我一直收到parsing error at '|'
啊。非常感谢!如果说'拥有'x'头部',我不会在某种意义上“返回”列表的列表,而只是列表? – ali
是的,这是问题所在。 –