[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
(from
http://hackage.haskell.org/package/base-4.2.0.1/docs/src/GHC-List.html)
-- | Extract the last element of a list, which must be finite and non-empty.
last :: [a] -> a
#ifdef USE_REPORT_PRELUDE
last [x] = x
last (_:xs) = last xs
last [] = errorEmptyList "last"
#else
-- eliminate repeated cases
last [] = errorEmptyList "last"
last (x:xs) = last' x xs
where last' y [] = y
last' _ (y:ys) = last' y ys
#endif
Question: What does the second "eliminate repeated cases" definition of
last gain compared to the first (simpler) one?
Thanks
Dimitri
More information about the Beginners
mailing list