【发布时间】:2010-10-05 06:04:14
【问题描述】:
我正在为 haskell 编写一个列表反转程序。
我已经有了列表反转的想法,这导致了以下代码:
myreverse list1
| list1 == [] = list1
| otherwise = (myreverse(tail list1)):(head list1)
不幸的是,上面的代码导致了以下错误:
Occurs check: cannot construct the infinite type: a = [[a]]
Expected type: [[a]]
Inferred type: a
In the second argument of '(:)', namely '(head list1)'
In the expression: (myreverse(tail list1)):(head list1)
PS:当我在我写的名为 mylast 的 sn-p 上运行它时,我得到了同样的错误,代码如下:
mylast list
| list == [] = []
| otherwise = mylast_helper(head list1)(tail list1)
mylast_helper item list2
| list2 == [] = item
| otherwise = mylast_helper(head list2)(tail list2)
在递归助手的其他情况下发生错误。
编辑:感谢所有输入,我想我忘了提到问题的约束禁止使用 ++ 运算符。我会牢记这一点,以备将来创建问题时使用。
干杯, -紫谷
【问题讨论】: