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

Henk-Jan van Tuyl hjgtuyl at chello.nl
Sat Oct 6 19:35:13 CEST 2012


On Sat, 06 Oct 2012 16:16:18 +0200, Manfred Lotz <manfred.lotz at arcor.de>  
wrote:

> 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?

You could use this:
   import Control.Monad
   myfor :: (a -> IO () ) -> [IO a] -> IO ()
   myfor f (x:xs) = mapM_ (liftM f) xs

or this:
   import Data.Functor
   myfor :: (a -> IO () ) -> [IO a] -> IO ()
   myfor f (x:xs) = mapM_ (f <$>) xs

For a description of liftM see:
   http://members.chello.nl/hjgtuyl/tourdemonad.html#liftM

Regards,
Henk-Jan van Tuyl


-- 
http://Van.Tuyl.eu/
http://members.chello.nl/hjgtuyl/tourdemonad.html
Haskell programming
--



More information about the Beginners mailing list