<div dir="ltr">putStrLn "Test" is a IO value, that prints the string "Test".<div><br></div><div>To sequence such monadic values, we use sequence or sequence_ (which ignores the results).</div><div><br></div><div>"sequence [a, b, c, d]" is equivalent to "a >> b >> c >> d"</div><div>"sequence_ [a, b, c, d]" is equivalent to "a >> b >> c >> d >> return ()"</div><div><br></div><div>There are similar functions mapM and mapM_, which can be used as follows.</div><div><br></div><div><font face="monospace, monospace">mapM f [a, b, c]</font></div><div><font face="monospace, monospace"> == sequence (map f [a,b,c])</font></div><div><font face="monospace, monospace"> == sequence [f a, f b, f c]</font></div><div><font face="monospace, monospace"> == f a >> f b >> f c</font></div><div><font face="monospace, monospace"><br></font></div>and,<div> <div><font face="monospace, monospace">mapM_ f [a, b, c] == f a >> f b >> f c >> return ()</font></div></div><div><font face="monospace, monospace"><br></font></div>From mapM_, we can create a convenience function forM_<div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">forM_ = flip mapM_</font></div><div><font face="monospace, monospace"><br></font></div><div>All the above are exported by <font face="monospace, monospace">Control.Monad</font></div></div><div class="gmail_extra"><br><div class="gmail_quote">On 1 May 2015 at 18:10, Shishir Srivastava <span dir="ltr"><<a href="mailto:shishir.srivastava@gmail.com" target="_blank">shishir.srivastava@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>Hi, </div><div><br></div><div>Please can anyone point out the correct way of doing this - I am simply trying to print "Test" in a loop based on the counter derived from the list - </div><div><br></div><div><font face="monospace, monospace">----</font></div><div><div><font face="monospace, monospace"> do</font></div><div><font face="monospace, monospace"> _ <- [1..4]</font></div><div><font face="monospace, monospace"> b <- putStrLn "Test"</font></div><div><font face="monospace, monospace"> return b</font></div></div><div><font face="monospace, monospace">---</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="arial, helvetica, sans-serif">The intended output is to print 'Test' 4 times.</font></div><div><font face="arial, helvetica, sans-serif"><br></font></div><div><font face="arial, helvetica, sans-serif">Clearly there is a mismatch between the Monad types but I can't see how to achieve this without causing the conflict.</font></div><div><font face="arial, helvetica, sans-serif"><br></font></div><div><font face="arial, helvetica, sans-serif">Thanks,</font></div><div><span style="font-family:georgia,serif;color:rgb(11,83,148)">Shishir</span></div>
</div>
<br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div>Regards</div><div dir="ltr"><div><br></div><div>Sumit Sahrawat</div></div></div></div></div></div></div>
</div>