99 questions/Solutions/5

From HaskellWiki
< 99 questions‎ | Solutions
Revision as of 19:54, 30 November 2012 by Davorak (talk | contribs) (tagged the implementation found in prelude.)
Jump to navigation Jump to search

(*) Reverse a list.

reverse          :: [a] -> [a]
reverse          =  foldl (flip (:)) []

The standard definition, found in the prelude, is concise, but not very readable. Another way to define reverse is:

reverse :: [a] -> [a]
reverse [] = []
reverse (x:xs) = reverse xs ++ [x]

However this definition is more wasteful than the one in Prelude as it repeatedly reconses the result as it is accumulated. The following variation avoids that, and thus computationally closer to the Prelude version.

reverse :: [a] -> [a]
reverse list = reverse' list []
  where
    reverse' [] reversed     = reversed
    reverse' (x:xs) reversed = reverse' xs (x:reversed)