At 1:29 PM +0100 2/4/11, Tim Baumgartner wrote:
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 help.
In your simplified example, it suffices to wrap
unsafeInterleaveIO around (mapM content ds). In a more realistic
case, you'd need to add some `unsafeInterleaveIO`s in `files` and
`directories`, too.
Dean
P.S. You might also want to look into iteratees, which accomplish
a similar goal in a more robust way.
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
Tim