Hi Stephen

Thank you very much, this indeed does the trick!
Using UnsafeIO, however, leaves a creepy unsafe feeling...
I don't fully understand though why it is unsafe. Doesn't hGetContents do the exact same thing (i.e. reading from IO returning a lazy string) but does not require UnsafeIO.

Fabian

On Wed, Feb 24, 2010 at 4:38 PM, Stephen Tetley <stephen.tetley@gmail.com> wrote:
Hi Fabian

You need to yield with unsafeInterleaveIO to allow some of the list to
be be consumed.

Something like this (which never terminates of course, but do produce output):


import System.IO.Unsafe
import Control.Monad

main = do messages <- readLazy
         mapM_ (\x -> putStr $ show x ++ "\n") $ messages
         return ()
         where
           readLazy :: IO [String]
           readLazy = unsafeInterleaveIO $ do
                        { c <- fancyIORead
                        ; liftM2 (++) (return c) readLazy
                        }
           fancyIORead :: IO [String]
           fancyIORead = return ["aa","bb"]