Make lines stricter to fix a space leak

Yitzchak Gale gale at
Mon Sep 27 13:40:52 EDT 2010

Daniel Fischer wrote:
> Proposal: A stricter implementation of lines.
> Reason: The current implementation causes a space leak
> Ticket:

I propose the following combinator approach as
an alternative:

lines :: String -> [String]
lines = map (takeWhile (/= '\n')) . takeWhile (not . null) .
            iterate (drop 1 . dropWhile (/= '\n'))

GHC fuses that into a tight loop. So in addition to solving
the space leak, it is faster and uses less heap than both
the existing implementation and Daniel's proposal.
It is also cleaner and easier to read, in my opinion.


More information about the Libraries mailing list