
5 Apr
2007
5 Apr
'07
12:59 a.m.
Marc Weber:
main = do lines <- liftM lines getContents mapM_ print lines -- *
So this example should "hang", as well, shouldn't it?
It would, except for the magic of unsafeInterleaveIO. It doesn't "hang" because getContents uses unsafeInterleaveIO internally to return the file contents lazily. If you took the unsafeInterleaveIO out of getContents, your program would wait for EOF on input before printing anything to output, as you had been expecting. Bulat's post showed how to add the same kind of magic into your use of mapM.