[Haskell-cafe] Reversing a string of words: C# v Perl V Ruby v Haskell

David Roundy droundy at darcs.net
Sun Dec 10 17:32:48 EST 2006

On Sun, Dec 10, 2006 at 10:50:38PM +1100, Andrew Savige wrote:
> Finally, and please don't laugh, in Haskell (GHC):
>  reverseWords :: String -> String
>  reverseWords xs = (concat (intersperse " " (reverse (words xs))))
> which does appear to work correctly. However, I would like to do
> justice to Haskell as a language -- and given that my clumsy
> Haskell is not written in expert style -- I'm hoping that someone
> on this list might offer a better, faster, or more idiomatic
> Haskell solution to this little problem.

reverseWords = unwords . reverse . words
David Roundy
Department of Physics
Oregon State University

More information about the Haskell-Cafe mailing list