how to apply a function which returns IO() to a list?

Hi, I have this function which write a file: writeHtml b x y = do writeFile (b ++ ".html") (htmlCode b x y) i need to apply to each member of a given list: the way i always try to codify functions in haskell is allways the same, the most primitive one and i think i cant use it here. writeList [] = ??? writeList (x:xs) = (writeHtml x) ??? How should i do this? Many thx, Nuno SAntos

Hi, I think the kind of structure you are after is something like:
writeList [] = return () writeList (x:xs) = do writeHtml x writeList xs
Which is already defined for you, using sequence_ Thanks Neil

Nuno,
I have this function which write a file:
writeHtml b x y = do writeFile (b ++ ".html") (htmlCode b x y)
i need to apply to each member of a given list:
Have a look at the mapM and mapM_ functions from the Prelude. Instantiated to IO, these have the following types: mapM :: (a -> IO b) -> [a] -> IO [b] mapM_ :: (a -> IO b) -> [a] -> m () HTH, Stefan

Nuno, Oops... Of course I meant mapM_ :: (a -> IO b) -> [a] -> IO () instead of mapM_ :: (a -> IO b) -> [a] -> m () Regards, Stefan

"developer" == developer
writes:
developer> Hi, I have this function which write a file: developer> writeHtml b x y = do writeFile (b ++ ".html") (htmlCode b x developer> y) developer> i need to apply to each member of a given list: developer> the way i always try to codify functions in haskell is developer> allways the same, the most primitive one and i think i cant developer> use it here. developer> writeList [] = ??? developer> writeList (x:xs) = (writeHtml x) ??? writeList [] = return () writeList (x:xs) = do writeHtml x writeList xs -- WBR, Max Vasin.
participants (4)
-
developer@imaginando.net
-
Max Vasin
-
Neil Mitchell
-
Stefan Holdermans