[Haskell-beginners] IO action on a list of [IO a]

Brent Yorgey byorgey at seas.upenn.edu
Sun Oct 7 19:27:30 CEST 2012


On Sun, Oct 07, 2012 at 08:41:32AM -0800, Christopher Howard wrote:
> On 10/06/2012 06:16 AM, Manfred Lotz wrote:
> > Hi all,
> > I want to do an IO action on a list of [IO a] like this:
> > 
> > myfor :: (a -> IO () ) -> [IO a] -> IO ()
> > myfor _ [] = return ()
> > myfor f (x:xs) = do 
> >   x' <- x
> >   f x'
> >   myfor f xs
> >     
> > 
> > 
> > Is there a library function doing just this?
> > 
> > 
> > 
> 
> For the curious newbies among us: Why would you want to perform an IO
> action on a list of IO actions?

"Perform an IO action on a list of IO actions" is kind of a poor way
to phrase it, which makes it sound arcane.  Here's a better way to say
what's going on: "take a list of IO actions, chain an additional
action onto the end of each, then run all the resulting actions in
sequence".

It's not really "performing an IO action on another" (which doesn't
even really make sense) but chaining IO actions together, using the
usual

  (>>=) :: IO a -> (a -> IO b) -> IO b

-Brent



More information about the Beginners mailing list