
I have a simple list like this: qlist = ["ll", "kkkkkkk", "ddddd"] Why is it OK to do this: qwrap [] = putStrLn ":" qwrap (a:as) = do putStrLn a qwrap as qwap qlist but not this: map putStrLn qlist What is going on in the map function? McQueen

Well, here is your problem:
map :: (a -> b) -> [a] -> [b]
putStrLn :: String -> IO ()
qlist :: [String]
map putStrLn :: [String] -> [IO ()]
map putStrLn qlist :: [IO ()]
The type of (map putStrLn qlist) is not of the form IO a, so it's not
an action which can be run. There are some useful prelude functions
called sequence and sequence_ whose types are as follows:
sequence :: (Monad m) => [m a] -> m [a]
sequence_ :: (Monad m) => [m a] -> m ()
they will put that list of actions into a single action which can then be run.
This pattern of mapping over a list and sequencing is so common that
it has a name:
mapM :: Monad m => (a -> m b) -> [a] -> m [b]
mapM_ :: Monad m => (a -> m b) -> [a] -> m ()
-- similar, but throws away computed results.
which is exactly what you're looking for. :)
- Cale
On 19/08/05, Brian McQueen
I have a simple list like this:
qlist = ["ll", "kkkkkkk", "ddddd"]
Why is it OK to do this:
qwrap [] = putStrLn ":" qwrap (a:as) = do putStrLn a qwrap as
qwap qlist
but not this:
map putStrLn qlist
What is going on in the map function?
McQueen _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (2)
-
Brian McQueen
-
Cale Gibbard