[Haskell-cafe] Lazy IO with recursive reads?
Fabian Roth
fabian.roth at gmail.com
Wed Feb 24 16:17:13 EST 2010
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 at 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"]
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20100224/5d3fbd5a/attachment.html
More information about the Haskell-Cafe
mailing list