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

Brian Hulley brianh at metamilk.com
Sun Dec 10 07:32:50 EST 2006


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.

What about just:

    reverseWords = concat . intersperse " " . reverse . words

-- 
http://www.metamilk.com


More information about the Haskell-Cafe mailing list