Desugar list comprehension

3 messages
Open this post in threaded view
|

Desugar list comprehension

 Hi, I have sumIs2020P1' xs = do x <- xs y <- xs guard (x + y == 2020) pure (x,y)which has been desugared from a list comprehensionI would like to reduce this even more using >>= So I dosumIs2020P1'' 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 ThanksMike Dr Mike Houghton[hidden email] _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
 If you check on hoogle for how guard is written, it is just this```guard True = `pure` () guard False = `empty``````That means you can use the same thing in your own codeimport Control.Applicativepairs 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 <[hidden email]> 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 comprehensionI would like to reduce this even more using >>= So I dosumIs2020P1'' 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 ThanksMike Dr Mike Houghton[hidden email] _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
 Duh!  Of course, thanks David. On 2 Dec 2020, at 12:51, David McBride <[hidden email]> wrote:If you check on hoogle for how guard is written, it is just this```guard True = `pure` () guard False = `empty``````That means you can use the same thing in your own codeimport Control.Applicativepairs 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 <[hidden email]> 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 comprehensionI would like to reduce this even more using >>= So I dosumIs2020P1'' 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 ThanksMike Dr Mike Houghton[hidden email] _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners _______________________________________________Beginners mailing list[hidden email]http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners_______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners