Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  Loop (Shishir Srivastava)
   2. Re:  Loop (Marcin Mrotek)
   3. Re:  Loop (Sumit Sahrawat, Maths & Computing, IIT (BHU))
   4. Re:  Loop (Shishir Srivastava)
   5. Re:  Loop (Marcin Mrotek)
   6. Re:  Loop ([email protected])
   7. Re:  Loop (Marcin Mrotek)
   8. Re:  Loop (Sumit Sahrawat, Maths & Computing, IIT (BHU))


----------------------------------------------------------------------

Message: 1
Date: Fri, 1 May 2015 13:40:16 +0100
From: Shishir Srivastava <[email protected]>
To: beginners <[email protected]>
Subject: [Haskell-beginners] Loop
Message-ID:
        <cale5rtsomdg9f349fwdkatwevxuqd71njuhtsvlw4s9y3vb...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150501/e7327970/attachment-0001.html>

------------------------------

Message: 2
Date: Fri, 1 May 2015 15:08:26 +0200
From: Marcin Mrotek <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Loop
Message-ID:
        <CAJcfPzntJ=4mYshZszaEGgMSW8icvLxe59-oO=yddzau+u6...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

Hello,

First of all, putStrLn has type IO (), so b is pretty useless in your
code, it's going to have type (), and the only value of this type is
(). The simplest way to perform some action based on a list and
discard the results is mapM_:

mapM_ (\_ -> putStrLn "Test") [1..4]

though if you discard the list's elements too, you can use sequence_
and replicate instead:

sequence_ . replicate 4 $ putStrLn "Test"

As a side note, if you actually want to combine monads (like list and
IO) you can use monad transformers. The ListT type from transformers
library has some issues as far as I know, but you can use the one from
pipes, for example. Have a look at:
http://www.haskellforall.com/2014/11/how-to-build-library-agnostic-streaming.html

Best regards,
Marcin Mrotek


------------------------------

Message: 3
Date: Fri, 1 May 2015 18:39:39 +0530
From: "Sumit Sahrawat, Maths & Computing, IIT (BHU)"
        <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Loop
Message-ID:
        <cajbew8mf_fgtp2sv5uedgxczrm26n1u_oltjcvsvrugb0j8...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

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 <[email protected]>
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
> [email protected]
> 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-0001.html>

------------------------------

Message: 4
Date: Fri, 1 May 2015 14:12:15 +0100
From: Shishir Srivastava <[email protected]>
To: beginners <[email protected]>
Subject: Re: [Haskell-beginners] Loop
Message-ID:
        <CALe5RTt5Jh8c18PNDdDeUThxFwTgiLXnh1=rxoi3c50snco...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Hi Marcin,

Thanks for your response. Yes my intention to use 'do' was to implement it
in so called 'imperative' style coding.

So if I understand it correctly there is no other way to achieve it via
'do' w/o using the monad transformers?

Thanks again
Shishir
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150501/147d3fc3/attachment-0001.html>

------------------------------

Message: 5
Date: Fri, 1 May 2015 16:23:51 +0200
From: Marcin Mrotek <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Loop
Message-ID:
        <cajcfpzn4qwlyugxtc0vkrbrn9cicvs3qq69+t4rh6a16afb...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

It depends on what are you trying to achieve. The ListT monad
transformer is an overkill for your example, as you don't use the
"non-determinism" feature of ListT (code written using it can use
whole lists of values as if it was one value with multiple "versions",
any action can return any number of results, which are then combined
together, perhaps without even calculating all of them if they're not
needed); if you just want to sequence actions, it's enough to use
mapM, mapM_, sequence, sequence_, and other functions from
Control.Monad. But if you have something more complicated in mind, it
might be worth taking a look at ListT and other transformers to see if
they can be of any use to you.

Best regards,
Marcin Mrotek


------------------------------

Message: 6
Date: Fri, 1 May 2015 13:36:42 -0400
From: [email protected]
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Cc: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Loop
Message-ID: <[email protected]>
Content-Type: text/plain;       charset=utf-8

What you really want is "replicateM_". But to answer your original question, 
you could do something like:

import Control.Monad.List
main = runListT $ do
  _ <- ListT $ pure [1,2,3,4]
  liftIO $ putStrLn "Test"

(replicateM_ is way better though)

Tom


El May 1, 2015, a las 10:23, Marcin Mrotek <[email protected]> 
escribi?:

> It depends on what are you trying to achieve. The ListT monad
> transformer is an overkill for your example, as you don't use the
> "non-determinism" feature of ListT (code written using it can use
> whole lists of values as if it was one value with multiple "versions",
> any action can return any number of results, which are then combined
> together, perhaps without even calculating all of them if they're not
> needed); if you just want to sequence actions, it's enough to use
> mapM, mapM_, sequence, sequence_, and other functions from
> Control.Monad. But if you have something more complicated in mind, it
> might be worth taking a look at ListT and other transformers to see if
> they can be of any use to you.
> 
> Best regards,
> Marcin Mrotek
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

Message: 7
Date: Fri, 1 May 2015 22:09:44 +0200
From: Marcin Mrotek <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Loop
Message-ID:
        <CAJcfPznFGE7LL-b9AGsybhK2Y68QVdDxAQ+MGmT=hpexecu...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

Ah, sorry, I forgot about replicateM_.

Best regards,
Marcin Mrotek


------------------------------

Message: 8
Date: Sat, 2 May 2015 05:36:47 +0530
From: "Sumit Sahrawat, Maths & Computing, IIT (BHU)"
        <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Loop
Message-ID:
        <CAJbEW8OVw-48rXAgfwwnjiOoMC2t=ullcvezf98ytd1-3sx...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

The reply I gave before might be more helpful if you consider the fact that

    a >> b >> c

is equivalent to

    do a
       b
       c

You can then use

    forM_ [1..4] (\_ -> putStrLn "Test")

What this does is,

    1. map (\_ -> putStrLn "Test") on [1..4]
    2. Use sequence_ to combine those operations

The even shorter way is replicateM_ which works like replicate from
Data.List

On 2 May 2015 at 01:39, Marcin Mrotek <[email protected]> wrote:

> Ah, sorry, I forgot about replicateM_.
>
> Best regards,
> Marcin Mrotek
> _______________________________________________
> Beginners mailing list
> [email protected]
> 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/20150502/06c988f9/attachment.html>

------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 83, Issue 1
****************************************

Reply via email to