[Haskell-cafe] Newbie type question for wxHaskell learner

Daniel Fischer daniel.is.fischer at googlemail.com
Fri Jan 7 17:21:09 CET 2011


On Friday 07 January 2011 17:09:11, b1g3ar5 wrote:
> I've tried to solve this but I am failing.
>
> I can do this:
>
> p0<-panel nb []
> e0<-textCtrl p [text:=my_list!!0]
>
> but I want to do this on all of my_list, so I tried:
>
> let es = map (\x-> textCtrl (panel nb []) [text:=x]) my_list
>
> Now, this won't work because the panel nb [] is IO (Panel()) and the
> parameter to textCtrl needs to be a Panel()
>
> How do I get out of IO?

What you need is mapM:

    es <- mapM (\x -> textCtrl (panel nb []) [text:=x]) my_list

mapM :: (Monad m) => (a -> m b) -> [a] -> m [b]

applies the function to each list element, runs the resulting action and 
collects the results. If you don't need the results but only the effects of 
running the actions (common in IO), use

mapM_ :: (Monad m) => (a -> m b) -> [a] -> m ()

mapM and mapM_ are compositions of

sequence :: (Monad m) => [m a] -> m [a]

resp.

sequence_ :: (Monad m) => [m a] -> m ()

with map (mapM f list === sequence (map f list)), those are useful on their 
own too.

>
> Thanks.



More information about the Haskell-Cafe mailing list