[Haskell-cafe] Lazy IO with recursive reads?

Ben Millwood haskell at benmachine.co.uk
Wed Feb 24 10:27:16 EST 2010

On Wed, Feb 24, 2010 at 1:53 PM, Fabian Roth <fabian.roth at gmail.com> wrote:
> Hi all,
> I am trying to read structured data from a socket and return a lazy list of
> records. However, the socket reading operation seems to be strict and never
> returns (until stack overflow).

This is expected behaviour. Normal sequencing of IO actions is done in
such a way as to preserve their order, which is obviously pretty
important if you want to ask for a response to your message after
you've sent it, rather than before. Lazy IO operations violate that
order and as a result are pretty scary and usually to be avoided. In
general, laziness only works well with pure functions where the order
doesn't matter because there are no observable side-effects.

There are ways of making IO lazy, but there are pretty much invariably
other ways of doing the same thing which result in fewer headaches
later on. I am hoping that other people more educated than I am will
be able to tell you about Iteratees and so forth.

More information about the Haskell-Cafe mailing list