[Haskell-beginners] lazy IO by example

Tim Baumgartner baumgartner.tim at googlemail.com
Fri Feb 4 13:29:49 CET 2011

Hi community,

In the following code, the main function only wants to print the first
5 numbers of a huge list. But since the computation is not lazy, this
doesn't work in a satisfactory way.

content :: Int -> IO [Int]
content i = do
  fs  <- files i
  ds  <- directories i
  fss <- mapM content ds
  return $ fs ++ concat fss

files i = return [1..i]

directories i = return [1..i-1]

main = content 1000000 >>= print . take 5

Now I'd like to know if it's possible to make this "IO" lazy, using
unsafeInterleaveIO. I tried to do it, failed. Perhaps someone can
I guess another weakness of this code is the bad performance of "dfs
++ concat fss", but currently I don't care about that.

Thanks for any help
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110204/4d81ea72/attachment.htm>

More information about the Beginners mailing list