[Haskell-cafe] Lazy IO with recursive reads?

Stephen Tetley stephen.tetley at gmail.com
Wed Feb 24 10:38:17 EST 2010


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"]


More information about the Haskell-Cafe mailing list