[Haskell-cafe] Re: IO in lists

Martin Huschenbett huschi at gmx.org
Wed Jan 17 17:00:19 EST 2007


Hi,

>> It's probably eaiser to work with normal lists:
>>
>> listChars :: IO [Char]
>> listChars = do
>>   c <- getChar
>>   if c == 'q'
>>     then return c
>>     else liftM2 (:) (return c) listChars
> 
> But that is not lazy any more, is it? The idea of the OT was, I think,
> that he can use the first elements of the list even before the last one
> was entered.

But it's possible to make it lazy again using 
System.IO.Unsafe.unsafeInterleaveIO:

listChars :: IO [Char]
listChars = unsafeInterleaveIO $ do
   c <- getChar
   if c == 'q'
     then return c
     else liftM2 (:) (return c) listChars


Regards,
   Martin.



More information about the Haskell-Cafe mailing list