[Haskell-beginners] Definition of last: why complicate it?

Dimitri DeFigueiredo defigueiredo at ucdavis.edu
Fri Apr 4 18:17:48 UTC 2014

I was looking at the definition of last in the prelude and saw this code


-- | Extract the last element of a list, which must be finite and non-empty.
last                    :: [a] -> a
last [x]                =  x
last (_:xs)             =  last xs
last []                 =  errorEmptyList "last"
-- eliminate repeated cases
last []                 =  errorEmptyList "last"
last (x:xs)             =  last' x xs
   where last' y []     = y
         last' _ (y:ys) = last' y ys

Question: What does the second "eliminate repeated cases" definition of 
last gain compared to the first (simpler) one?



More information about the Beginners mailing list