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

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

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

sinosoidal
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


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

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

Neil Mitchell
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
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

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

Stefan Holdermans
In reply to this post by sinosoidal
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
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

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

Stefan Holdermans
Nuno,

Oops... Of course I meant

   mapM_ :: (a -> IO b) -> [a] -> IO ()

instead of

   mapM_ :: (a -> IO b) -> [a] -> m ()

Regards,

   Stefan
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

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

Max Vasin
In reply to this post by sinosoidal
>>>>> "developer" == developer  <[hidden email]> 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.



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe