I was able to execute the following code flawlessly
myLast :: [a] -> a
myLast [] = error \"Can\'t call myLast on an empty list!\"
myLast (x:_) = x
You're declaring the input to be a list of type [a]
, and the rest to be of type a
.
A list of type [a]
in Haskell consists of a head of type a
and a tail, a list of type [a]
. The cons constructor :
takes the head and tail as its arguments.
When you deconstruct a list as (x:y)
, x
is the head and y
is the tail. So in your second code fragment, you're binding the tail of the list, which has the list type [a]
, when your type signature requires that you return a value of type a
(the head being one example).