此代码完美无缺:
last' :: [a] -> a
last' (x:[]) = x
last' (x:xs) = last xs
但如果我尝试添加:
last' [] = error "Empty list"
任何地方。我收到这个错误:
"Couldn't match type 'a' with [Char] -> a0'
'a' is a rigid type variable bound by the type signature for last' :: [a] -> a
In the expression: error
in any equation for last': last [] = error "Empty list"
这是为什么?当我为empy列表添加案例时,我的head,tail和init的实现并没有尖叫。
我是个白痴。我有错别字。谢谢!答案 0 :(得分:2)
添加error
并没有破坏您的代码。这应该是正确的实现:
last' :: [a] -> a
last' (x:[]) = x
last' (x:xs) = last' xs -- xs not x
last' [] = error "Empty list"