[Haskell-beginners] get rid of IO in [IO XXX]

Alex Belanger i.caught.air at gmail.com
Mon Sep 4 16:38:25 UTC 2017


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, <info at maximka.de> wrote:

> What is the way to transform a list of [IO XXX] type to [XXX]?
>
> Thanks,
> Alexei
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20170904/9001616c/attachment.html>


More information about the Beginners mailing list