[Haskell-beginners] Prelude Implementation of Last overlapping patterns?

Mark Stoehr stoehr at cs.uchicago.edu
Sat Dec 24 18:48:25 CET 2011


The standard Prelude implementation of 'last' is as follows
\begin{code}
last             :: [a] -> a
last [x]         =  x
last (_:xs)      =  last xs
last []          =  error "Prelude.last: empty list"
\end{code}

Isn't [x] equivalent to (x:[]) hence wouldn't [1] match both [x] and
(_:xs)?  If that's the case then we would have
last [1] == 1
and
last [1] == last []

but that doesn't happen. Why?



More information about the Beginners mailing list