[Haskell-beginners] Reversing the lines read from stdin

Krzysztof Skrzętnicki gtener at gmail.com
Thu Jan 28 15:57:55 EST 2010


On Thu, Jan 28, 2010 at 21:44, Tim Perry <perry2of5 at yahoo.com> wrote:
>
> I wrote a simple program: it reads the contents from standard in and
> reverses the order of the lines. I imagine there are many cleaner ways to do
> this. Anybody willing to post some rewrites?
>
> Thanks,
> Tim
>
>
> main = interact reverseFile
> reverseFile s = unlines $ lines $ foldr combineStr [] (lines s)
> combineStr s1 s2 = s2 ++ "\n" ++ s1
>

What about this version:

main = interact (unlines . reverse . lines)

Best regards
Krzysztof Skrzętnicki


More information about the Beginners mailing list