[Haskell-cafe] Re: Trying to write a TCP proxy

Tomasz Zielonka tomasz.zielonka at gmail.com
Wed Sep 20 13:10:43 EDT 2006

On Thu, Sep 21, 2006 at 12:26:03AM +1000, John Ky wrote:
> Given that putStrLn contents did manage to print out the HTTP header
> before blocking, am I correct in coming to the conlusion that
> 'contents' is evaluated lazily?

hGetContents breaks the rules of the IO monad - it returns a value (the
contents list) that is not entirely pure - evaluation of this value
causes execution of IO actions, namely reading from the handle. This way
of operation is not a rule, but an exception.

BTW, try to implement hGetContents in pure Haskell 98, using only the
functions specified there, but excluding (hGetContents, getContents,
readFile and interact). As a test try this code on a big or infinite
input, like /dev/zero:

    import IO
    main = hGetContents stdin >>= print . take 100

hGetContents may cause some confusion, but it also is very handy. It
lets you treat file contents as a lazy list, which allows you to use
very high-level code to process the data without having to load the
whole file into memory.

Best regards

More information about the Haskell-Cafe mailing list