Discussion:
[Haskell-beginners] combine concatMap and mapM
Renah Scarowsky
2014-02-17 10:38:47 UTC
Permalink
Hi,

How would I combine the functionalities of concatMap and mapM to define a function like:
Monad m => (a -> m [b]) -> [a] -> m [b]

I need to create a recursive function within the IO monad which results in a list of the original type.
So something like:
goX :: MonadIO m => x -> m [x]
goX = do
....
x includes within it a list of xs, let's call it (children x)
I need to call goX on each of the children and get a modified list of children as result
ys <- ?
return $ x { children = ys } : zs


Thanks,

Renah Scarowsky
Suite Solutions
Create>Manage>Deploy
http://www.suite-sol.com<http://www.suite-sol.com/>
Vlatko Basic
2014-02-17 12:42:38 UTC
Permalink
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body style="background-color: rgb(255, 255, 255); color: rgb(0, 0,
0);" bgcolor="#FFFFFF" text="#000000">
Hi Renah,<br>
<br>
don't know if I understood correctly, but this might help:<br>
<br>
Long version:<br>
<br>
fun' :: MonadIO m =&gt; (a -&gt; m [b]) -&gt; [a] -&gt; m [b]<br>
fun' f cs = do<br>
&nbsp; results &lt;- mapM f cs<br>
&nbsp; let merged = concat results<br>
&nbsp; return merged<br>
<br>
<br>
Shorter version with fmap:<br>
<br>
import&nbsp; Control.Applicative ((&lt;$&gt;))<br>
<br>
fun :: (MonadIO m, Functor m) =&gt; (a -&gt; m [b]) -&gt; [a] -&gt;
m [b]<br>
fun f cs = concat &lt;$&gt; mapM f cs<br>
<br>
<br>
Best regards,<br>
<br>
vlatko<br>
<br>
<blockquote style="border-left: 2px solid #330033 !important;
border-right: 2px solid #330033 !important; padding: 0px 15px 0px
15px; margin: 8px 2px;"
cite="mid:***@DB4PR05MB271.eurprd05.prod.outlook.com"
type="cite"><!--[if !IE]><DIV style="border-left: 2px solid #330033; border-right: 2px solid #330033; padding: 0px 15px; margin: 2px 0px;"><![endif]--><span
style="color:#000000;" class="headerSpan">
<div class="moz-cite-prefix">-------- Original Message --------<br>
Subject: [Haskell-beginners] combine concatMap and mapM<br>
From: Renah Scarowsky <a class="moz-txt-link-rfc2396E" href="mailto:***@suite-sol.com">&lt;***@suite-sol.com&gt;</a><br>
To: <a class="moz-txt-link-abbreviated" href="mailto:***@haskell.org">***@haskell.org</a> <a class="moz-txt-link-rfc2396E" href="mailto:***@haskell.org">&lt;***@haskell.org&gt;</a><br>
Date: 17.02.2014 11:38<br>
</div>
<br>
<br>
</span>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<meta name="Generator" content="Microsoft Word 12 (filtered
medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
span.EmailStyle17
{mso-style-type:personal-compose;
font-family:"Calibri","sans-serif";
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
<div class="WordSection1">
<p class="MsoNormal">Hi,<o:p></o:p></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">How would I combine the functionalities of
concatMap and mapM to define a function like:<o:p></o:p></p> <p class="MsoNormal">Monad m =&gt; (a&nbsp; -&gt; m [b]) -&gt; [a]
-&gt; m [b]<o:p></o:p></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">I need to create a recursive function
within the IO monad which results in a list of the original
type.<o:p></o:p></p> <p class="MsoNormal">So something like:<o:p></o:p></p> <p class="MsoNormal">goX :: MonadIO m =&gt; x -&gt; m [x]<o:p></o:p></p>
<p class="MsoNormal">goX = do<o:p></o:p></p>
<p class="MsoNormal">&#8230;.<o:p></o:p></p>
<p class="MsoNormal">x includes within it a list of xs, let's
call it (children x)<o:p></o:p></p>
<p class="MsoNormal">I need to call goX on each of the children
and get a modified list of children as result<br>
ys &lt;- <span style="background:yellow;mso-highlight:yellow">?</span><o:p></o:p></p> <p class="MsoNormal">return $ x { children = ys } : zs<o:p></o:p></p> <p class="MsoNormal"><o:p>&nbsp;</o:p></p> <p class="MsoNormal"><o:p>&nbsp;</o:p></p> <p class="MsoNormal">Thanks,<o:p></o:p></p> <p class="MsoNormal"><o:p>&nbsp;</o:p></p> <p class="MsoNormal">Renah Scarowsky<o:p></o:p></p> <p class="MsoNormal">Suite Solutions<o:p></o:p></p> <p class="MsoNormal"><i>Create&gt;Manage&gt;Deploy<o:p></o:p></i></p>
<p class="MsoNormal"><a moz-do-not-send="true"
href="http://www.suite-sol.com/"><span
style="color:windowtext">http://www.suite-sol.com</span></a><o:p></o:p></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
Beginners mailing list
<a class="moz-txt-link-abbreviated" href="mailto:***@haskell.org">***@haskell.org</a>
<a class="moz-txt-link-freetext" href="http://www.haskell.org/mailman/listinfo/beginners">http://www.haskell.org/mailman/listinfo/beginners</a>
</pre>
<!--[if !IE]></DIV><![endif]--></blockquote>
<br>
</body>
</html>

Loading...