Lazy streams and unsafeInterleaveIO

Jyrinx jyrinx_list@mindspring.com
Sun, 22 Dec 2002 18:32:43 -0800


Hal Daume III wrote:

>>BTW, I already found a major problem with the code I attached earlier, 
>>using unsafeInterleaveIO: Run in GHCi (as I had done), it works fine; 
>>but compiled by GHC and run as an executable, it waits for input and 
>>*then* displays the prompt after the user hits Enter ... not very 
>>helpful. I didn't think it would do that, since (putStr "? " >> readLn) 
>>seemed pretty explicit as to order of evaluation, but I guess that's 
>>what I get for breaking referential transparency ...
>>    
>>
>
>You probably want to set the buffering otherwise. [...]
>  
>
Ah, you're right ... I changed (putStr "? " >> readLn) to (putStr "? " 
 >> hFlush stdout >> readLn) and it worked.

So is this lazy-stream-via-unsafeInterleaveIO not so nasty, then, so 
long as a few precautions (not reading too far into the stream, 
accounting for buffering, etc.) are taken? I like the idiom Hudak uses 
(passing a stream of I/O results to the purely functional part of the 
program), so if it's kosher enough I'd like to get hacking elsewhere ...

Luke Maurer
jyrinx_list@mindspring.com