我正试图围绕 Haskell 的语法。
这个问题在逻辑上很容易解决。我必须分解正整数和负整数的列表并将它们分组,以便
[1,2,3,-1,-2,-3,1,2,3] 变为 [[1,2,3],[-1,-2,-3], [1,2,3] ]
我想使用一个更高阶的函数 foldr ,以便能够通过一个接受两个争论的匿名函数来做到这一点。
这就是我到目前为止所拥有的。
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 行出现类型错误?
我不是将整数 (x) 连接到整数列表 (head y)
2)你如何使用守卫写出条件?我试过这样做,但我一直在parsing error at '|'