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

Malcolm Wallace
Tue, 6 Nov 2001 10:39:39 +0000

> 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?
> The problem with
>     sequence :: (Monad m) => [m a] -> m [a]
>     sequence = foldr (liftM2 (:)) (return []) where
> 	liftM2 f a b = do { a' <- a; b' <- b; return (f a' b') }
> is that it consumes the entire input before producing a result.

Are you *really* sure that the latter consumes its entire input
before producing any result?  It looks pretty lazy to me, and I seem
to be able to give it an infinite list of computations and yet start
receiving results immediately....