为什么添加错误案例会破坏我对last的实现



此代码运行良好:

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的实现并没有发出刺耳的声音。

我是个白痴。我有打字错误。谢谢

添加error不会破坏代码。这应该是正确的实现:

last' :: [a] -> a
last' (x:[]) = x
last' (x:xs) = last' xs  -- xs not x
last' [] = error "Empty list"

最新更新