Send Beginners mailing list submissions to
        beginners@haskell.org

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
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

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


Today's Topics:

   1. Re:  Desugar list comprehension (David McBride)
   2. Re:  Desugar list comprehension (mike h)


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

Message: 1
Date: Wed, 2 Dec 2020 07:51:40 -0500
From: David McBride <toa...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Desugar list comprehension
Message-ID:
        <CAN+Tr42Mx0XWDXg9T-FyWD_r5+K-7dCookK=ZkqwizVmxw64=a...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

If you check on hoogle for how guard is written, it is just this

guard True  = pure
<https://hackage.haskell.org/package/base-4.14.0.0/docs/Control-Applicative.html#v:pure>
()
guard False = empty
<https://hackage.haskell.org/package/base-4.14.0.0/docs/Control-Applicative.html#v:empty>

That means you can use the same thing in your own code

import Control.Applicative

pairs xs =
  xs >>= \x ->
    xs >>= \y ->
      if (x + y == 2020) then pure (x,y) else empty


On Wed, Dec 2, 2020 at 5:31 AM mike h <mike_k_hough...@yahoo.co.uk> wrote:

> Hi,
>  I have
> sumIs2020P1' xs = do
> x <- xs
> y <- xs
> guard (x + y == 2020)
> pure (x,y)
>
> which has been desugared from a list comprehension
> I would like to reduce this even more using >>=
> So I do
> sumIs2020P1'' xs = (a,b) where
> (a,b):rest = filter (\(x,y) -> x + y == 2020) pairs
>
> pairs = xs >>= \x ->
> xs >>= \y ->
> pure (x,y)
>
> but really I would like the guard to be within the >>= sections but I
> could not work out
> how to do it!
> i.e. I’m looking for something like (pseudo code)
>
> pairs = xs >>= \x ->
> xs >>= \y ->
> if (x + y == 2020) then pure (x,y) else DO_NOTHING
> which would then allow the filter to be removed.
>
>
> Many Thanks
>
> Mike
>
>
>
> Dr Mike Houghton
>
> mike_k_hough...@yahoo.co.uk
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20201202/c6c160ce/attachment-0001.html>

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

Message: 2
Date: Wed, 2 Dec 2020 15:47:05 +0000
From: mike h <mike_k_hough...@yahoo.co.uk>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Desugar list comprehension
Message-ID: <75d621d3-e4dd-4777-9806-5be28a375...@yahoo.co.uk>
Content-Type: text/plain; charset="utf-8"


Duh!  Of course, thanks David.



> On 2 Dec 2020, at 12:51, David McBride <toa...@gmail.com> wrote:
> 
> If you check on hoogle for how guard is written, it is just this
> guard True  = pure 
> <https://hackage.haskell.org/package/base-4.14.0.0/docs/Control-Applicative.html#v:pure>
>  ()
> guard False = empty 
> <https://hackage.haskell.org/package/base-4.14.0.0/docs/Control-Applicative.html#v:empty>
> That means you can use the same thing in your own code
> 
> import Control.Applicative
> 
> pairs xs =
>   xs >>= \x ->
>     xs >>= \y ->
>       if (x + y == 2020) then pure (x,y) else empty
> 
> On Wed, Dec 2, 2020 at 5:31 AM mike h <mike_k_hough...@yahoo.co.uk 
> <mailto:mike_k_hough...@yahoo.co.uk>> wrote:
> Hi,
>  I have 
> sumIs2020P1' xs = do 
>     x <- xs
>     y <- xs
>     guard (x + y == 2020)
>     pure (x,y)
> 
> which has been desugared from a list comprehension
> I would like to reduce this even more using >>= 
> So I do
> sumIs2020P1'' xs =  (a,b) where
>     (a,b):rest = filter (\(x,y) -> x + y == 2020)  pairs       
> 
>     pairs = xs >>= \x -> 
>                     xs >>= \y -> 
>                         pure (x,y)
> 
> but really I would like the guard to be within the >>= sections but I could 
> not work out 
> how to do it!
> i.e. I’m looking for something like (pseudo code)
> 
> pairs = xs >>= \x -> 
>                     xs >>= \y -> 
>                       if (x + y == 2020) then pure (x,y) else DO_NOTHING
>                         
> which would then allow the filter to be removed. 
> 
> 
> Many Thanks
> 
> Mike
> 
> 
> 
> Dr Mike Houghton
> 
> mike_k_hough...@yahoo.co.uk <mailto:mike_k_hough...@yahoo.co.uk>
> 
> 
> 
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org <mailto:Beginners@haskell.org>
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners 
> <http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners>
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20201202/876d56df/attachment-0001.html>

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

Subject: Digest Footer

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


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

End of Beginners Digest, Vol 149, Issue 2
*****************************************

Reply via email to