我在做Set7.hs,来自haskell.mooc.fi/文件7
-- Ex 5: reverse a NonEmpty list.
--
-- PS. The Data.List.NonEmpty type has been imported for you
-- below doesn't work
-- reverseNonEmpty :: NonEmpty a -> NonEmpty a
-- reverseNonEmpty (h:|tail) = head reversed :| (tail reversed)
-- where
-- reversed = reverse (h:tail)
-- below works
reverseNonEmpty :: NonEmpty a -> NonEmpty a
reverseNonEmpty (h :| tail) = head reversed :| revTail
where
reversed = reverse (h : tail)
-- revTail = (tail reversed) - this doesn't work, but WHY???
(_:revTail) = reversed
字符串
错误消息为:
Set7.hs:152:15: error:
• Couldn't match expected type ‘[a] -> t’ with actual type ‘[a]’
• The function ‘tail’ is applied to one argument,
but its type ‘[a]’ has none
In the expression: tail reversed
In an equation for ‘revTail’: revTail = tail reversed
• Relevant bindings include
revTail :: t (bound at Set7.hs:152:5)
reversed :: [a] (bound at Set7.hs:151:5)
tail :: [a] (bound at Set7.hs:149:23)
h :: a (bound at Set7.hs:149:18)
reverseNonEmpty :: NonEmpty a -> NonEmpty a
(bound at Set7.hs:149:1)
|
152 | revTail = tail reversed
型
我不明白错误消息,tail reversed
有什么问题?
为什么使用模式匹配(_:revTail) = reversed
的版本可以工作?
3条答案
按热度按时间liwlm1x91#
第一个月
你自己绑定了名字
tail
,引用NonEmpty
的列表部分。所以这隐藏了普通的tail
函数,并且没有办法在reverseNonEmpty
体内引用它。这就是为什么错误消息告诉您
tail
的类型为[a]
,并且不能应用于参数。camsedfj2#
为了避免这些问题,我强烈建议您在编译代码时启用警告,您可以使用GHC标志
-Wall
。随着警告的出现,GHC发现了这个问题:
字符串
GHC在这里警告作用域中有两个东西,都命名为
tail
。一个是库函数,另一个是通过模式匹配绑定的局部变量。50few1ms3#
就像其他人已经说过的。请不要使用与外部作用域中已经存在的变量同名的变量名。您将
tail
分配给输入的尾部,所以这将不起作用。然而,我认为我的意图是通过递归写相反的。我们可以递归:
字符串
在这里,我把实现
…
部分作为练习。