Haskell列表反转错误 [英] Haskell List Reversal Error
问题描述
我已经知道列表反转,并且导致了以下代码:
p> myreverse list1
| list1 == [] = list1
|否则=(myreverse(尾列表1)):(头列表1)
不幸的是,上面的代码导致以下错误:
发生检查:无法构造无限类型:a = [[a]]
预期类型: [[a]]
推断类型:a
在'(:)'的第二个参数中,即'(head list1)'
在表达式中:(myreverse(tail list1)) :(head list1)
PS:当我在代码片段上运行时,出现同样的错误我写下mylast编码如下:
mylast list
| list == [] = []
|否则= mylast_helper(头部列表1)(尾部列表1)
mylast_helper项目列表2
| list2 == [] = item
|否则= mylast_helper(head list2)(tail list2)
在递归辅助函数的情况下发生错误。
编辑:感谢所有的输入,我想我忘了提及问题的约束禁止使用++运算符。我会记住我今后创建的问题。
干杯,
-Zigu
您正在使用函数
(:) :: a - > [a] - > [b]
带有不正确的参数:
myReverse(tail list1):: [a]
head list1 :: a
在你的函数中,列表 list1 必须有 a 类型。因此,第二个参数
必须具有类型 [a] 。 GHC警告你它不能构建你指定的类型。列表的头部在结构上比列表的尾部小,但是您告诉它列表的头部具有类型 [a] ,但列表的尾部具有类型 a 。然而,如果你仔细观察你的类型,你会注意到你可以将的头添加使用(++)递归调用 myreverse :> list1 :
myReverse xs = case(null xs)of
True - > xs
False - >
这里,
[head xs] :: [a]
myReverse(tail xs):: [a]
符合追加的类型:
I'm writing a list reversal program for haskell. I've got the idea for the list reversal and that has lead to the following code: Unfortunately the above code results in the following error: PS: I get the same sort of error when I run it on a snippet that I wrote called mylast coded below: Error occurs at the otherwise case of the recursive helper. EDIT: Thanks for all the input, I guess I forgot to mention that the constraints of the question forbid the use of the ++ operator. I'll keep that in mind for future questions I create. Cheers,
-Zigu You are using the function with ill-typed arguments: In your function, the list list1 must have type a. Hence the second argument, head list1, must have type [a]. GHC is warning you that it cannot construct the type you have specified for it. The head of a list is structurally smaller than the tail of a list, but you are telling it that the head of a list has type [a], yet the tail of a list has type a. If you stare closely at your types, however, you will notice that you can append the head of list1 to the recursive call to myreverse using (++): Here, which aligns with the type of append: There are much better ways to implement reverse, however. The Prelude defines reverse as a left fold (. Another version of reverse can be implemented using a right fold, and is very similar to your myReverse function:
这篇关于Haskell列表反转错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!(++):: [a] - > [a] - >然而,实现 reverse 有很多更好的方法。 Prelude将 reverse 定义为左侧折叠(。 reverse 的另一个版本可以使用右侧折叠实现,与您的 myReverse 非常相似。函数:
$ b $ pre $ reverse xs = foldr(\ x xs - > xs ++ [x])[] xs
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)
mylast list
| list == [] = []
| otherwise = mylast_helper(head list1)(tail list1)
mylast_helper item list2
| list2 == [] = item
| otherwise = mylast_helper(head list2)(tail list2)
(:) :: a -> [a] -> [a]
myReverse (tail list1) :: [a]
head list1 :: a
myReverse xs = case (null xs) of
True -> xs
False -> myReverse (tail xs) ++ [head xs]
[head xs] :: [a]
myReverse (tail xs) :: [a]
(++) :: [a] -> [a] -> [a]
reverse xs = foldr (\x xs -> xs ++ [x]) [] xs