how to do lazy IO like getContents?

Ben Escoto bescoto at
Sat Oct 18 17:50:22 EDT 2003

Hi, I'm trying to do IO and it isn't lazy enough.  My problem seems
similar to this one:  Write a function that lazily reads characters
from stdin, just like getContents does.  These two don't work:

    lazyRead :: IO String
    lazyRead = do first_char <- getChar
                  rest <- lazyRead
                  return $ first_char:rest

    lazyRead2 :: IO String
    lazyRead2 = (liftM2 (:)) getChar lazyRead2

because neither has the intended behavior, because if I type:

    lazyRead >>= putChar . head

Then it seems that all of stdin is read, not just the first character,
which is different from:

    getContents >>= putChar . head

which only reads one character.  So how do you write getContents in
haskell?  Thanks for any insight.

Ben Escoto
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url :

More information about the Haskell mailing list