Reputation: 982
This code works perfectly fine:
last' :: [a] -> a
last' (x:[]) = x
last' (x:xs) = last xs
But if I try to add:
last' [] = error "Empty list"
Anywhere. I get this error:
"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"
Why is this? My implementations of head, tail and init did not scream when I put in the case for the empy list.
I'm an idiot. I had typos. Thanks!
Upvotes: 0
Views: 66
Reputation: 48766
Adding error
is not breaking your code. This should be the correct implementation:
last' :: [a] -> a
last' (x:[]) = x
last' (x:xs) = last' xs -- xs not x
last' [] = error "Empty list"
Upvotes: 2