Non-monolithic version of 'sequence' for IO monad?

Lauri Alanko la@iki.fi
Tue, 6 Nov 2001 02:53:13 +0200


On Tue, Nov 06, 2001 at 12:09:58AM +0000, Marcin 'Qrczak' Kowalczyk wrote:
> Mon, 05 Nov 2001 14:23:18 -0800, Joe English <jenglish@flightlab.com> pisze:
> 
> > Is there any way, short of using unsafePerformIO,
> > to implement a combinator
> > 
> >     f :: [IO a] -> IO [a]
> > 
> > in a way that the the result is produced lazily?
> 
> No. If evaluation of list elements causes IO, then it's unsafe.

At least one can use unsafeInterleaveIO, which is marginally safer.

-- Result is evaluated as it is needed, but the internal ordering of the
-- sequenced actions is retained
unsafeLazySequence 
    = foldr (\m l -> unsafeInterleaveIO (liftM2 (:) m l)) (return [])

-- Just a list of results of the actions, no ordering is guaranteed
unsafeLazierSequence 
    = foldr (\m l -> liftM2 (:) (unsafeInterleaveIO m) l) (return [])

What, by the way, is the practical difference between unsafeInterleaveIO
and (return . unsafePerformIO)?


Lauri Alanko
la@iki.fi