get rid of IO in [IO XXX]

What is the way to transform a list of [IO XXX] type to [XXX]? Thanks, Alexei

Hi,
Use sequence. It has type t (m a) -> m (t a) where m is a Monad (like
IO) and t is a Traversable (like the list).
Documentation: http://hackage.haskell.org/package/base-4.10.0.0/docs/Prelude.html#v:sequenc...
I got to it using Hoogle:
https://www.haskell.org/hoogle/?hoogle=%5BIO+a%5D+-%3E+IO+%5Ba%5D
On Mon, Sep 4, 2017 at 9:04 AM,
What is the way to transform a list of [IO XXX] type to [XXX]?
Thanks, Alexei _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
-- Mihai Maruseac (MM) "If you can't solve a problem, then there's an easier problem you can solve: find it." -- George Polya

You can turn `[IO a]` into `IO [a]` by traversing the Traversable and
sequencing the actions using `sequence`.
Note that what it creates is a slightly different IO computation that
re-organizes the results, you'll still need to run that IO in the end.
Typically, it's passed all the way down to your closest use of IO (often
main for beginners) where you'll be able to finally get rid of it by
performing the effects and doing something with the results. The flavor of
choice depends on the situation but I find the IO monad very readable.
main :: IO ()
main = do
listOfA <- sequence ioListOfA
-- Use listOfA from here
Alex
On Sep 4, 2017 12:05 PM,
What is the way to transform a list of [IO XXX] type to [XXX]?
Thanks, Alexei _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
participants (3)
-
Alex Belanger
-
info@maximka.de
-
Mihai Maruseac