IO action on a list of [IO a]

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? -- Thanks, Manfred

On 06.10.2012 16:16, 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?
You could do: mapM_ (\x -> x >>= f) xs

On Sat, 06 Oct 2012 16:56:36 +0200
koomi
On 06.10.2012 16:16, 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?
You could do: mapM_ (\x -> x >>= f) xs
Yep, that works nicely. Thanks. -- Manfred

On Sat, 06 Oct 2012 16:16:18 +0200, Manfred Lotz
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 --

On Sat, 06 Oct 2012 19:35:13 +0200
"Henk-Jan van Tuyl"
On Sat, 06 Oct 2012 16:16:18 +0200, Manfred Lotz
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
Shouldn't it be xs instead of (x:xs)? The signatures are the same as in my own myfor. However, when I run my code using your versions of myfor I do not get any output at all. -- Manfred -- Manfred

On Sat, Oct 06, 2012 at 07:35:13PM +0200, Henk-Jan van Tuyl wrote:
On Sat, 06 Oct 2012 16:16:18 +0200, Manfred Lotz
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
This should be myfor f xs = mapM_ (>>= f) xs using (liftM f) will result in the right type but it does the wrong thing, as Manfred observed: f :: a -> IO () liftM f :: IO a -> IO (IO a) So mapping liftM f over a list of IO actions results in a list of IO actions with no effects, whose results are the IO actions you really wanted. Then mapM_ throws away those IO actions you really wanted, resulting in essentially (return () :: IO ()). -Brent

Here is my try. It works for any monad (not just IO) and any foldable
(not just lists).
import Data.Foldable
myfor :: (Monad m, Foldable t) => (a -> m ()) -> t (m a) -> m ()
myfor f = foldlM (flip $ const (>>=f)) ()
--
~ ugo pozo
~ mailto:ugopozo@gmail.com
~ home://11.3266.2021
~ cell://11.8432.9252
On Sat, Oct 6, 2012 at 9:08 PM, Brent Yorgey
On Sat, Oct 06, 2012 at 07:35:13PM +0200, Henk-Jan van Tuyl wrote:
On Sat, 06 Oct 2012 16:16:18 +0200, Manfred Lotz
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
This should be
myfor f xs = mapM_ (>>= f) xs
using (liftM f) will result in the right type but it does the wrong thing, as Manfred observed:
f :: a -> IO () liftM f :: IO a -> IO (IO a)
So mapping liftM f over a list of IO actions results in a list of IO actions with no effects, whose results are the IO actions you really wanted. Then mapM_ throws away those IO actions you really wanted, resulting in essentially (return () :: IO ()).
-Brent
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

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? -- frigidcode.com indicium.us

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

On 10/07/2012 12:41 PM, Christopher Howard wrote:
For the curious newbies among us: Why would you want to perform an IO action on a list of IO actions?
Let's say you have a list of URLs, and you map, downloadUrl :: URL -> IO Stuff over that list. The result will be a list, [IO Stuff]. Now suppose you want to print them all one at a time. Printing is an IO action, so this is performing an IO action (print) on a list of IO "actions" (downloaded URLs).
participants (7)
-
Brent Yorgey
-
Christopher Howard
-
Henk-Jan van Tuyl
-
koomi
-
Manfred Lotz
-
Michael Orlitzky
-
ugo pozo