[Haskell-beginners] Loop
Sumit Sahrawat, Maths & Computing, IIT (BHU)
sumit.sahrawat.apm13 at iitbhu.ac.in
Fri May 1 13:09:39 UTC 2015
putStrLn "Test" is a IO value, that prints the string "Test".
To sequence such monadic values, we use sequence or sequence_ (which
ignores the results).
"sequence [a, b, c, d]" is equivalent to "a >> b >> c >> d"
"sequence_ [a, b, c, d]" is equivalent to "a >> b >> c >> d >> return ()"
There are similar functions mapM and mapM_, which can be used as follows.
mapM f [a, b, c]
== sequence (map f [a,b,c])
== sequence [f a, f b, f c]
== f a >> f b >> f c
and,
mapM_ f [a, b, c] == f a >> f b >> f c >> return ()
>From mapM_, we can create a convenience function forM_
forM_ = flip mapM_
All the above are exported by Control.Monad
On 1 May 2015 at 18:10, Shishir Srivastava <shishir.srivastava at gmail.com>
wrote:
> Hi,
>
> 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
> -
>
> ----
> do
> _ <- [1..4]
> b <- putStrLn "Test"
> return b
> ---
>
> The intended output is to print 'Test' 4 times.
>
> Clearly there is a mismatch between the Monad types but I can't see how to
> achieve this without causing the conflict.
>
> Thanks,
> Shishir
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
--
Regards
Sumit Sahrawat
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150501/807bf345/attachment.html>
More information about the Beginners
mailing list