<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div class="">
<div>Duh!  Of course, thanks David.</div><div><br class=""></div><br class="Apple-interchange-newline">

</div>
<div><br class=""><blockquote type="cite" class=""><div class="">On 2 Dec 2020, at 12:51, David McBride <<a href="mailto:toad3k@gmail.com" class="">toad3k@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="">If you check on hoogle for how guard is written, it is just this</div><div class=""></div><div class=""><pre class="">guard True  = <code class=""><a href="https://hackage.haskell.org/package/base-4.14.0.0/docs/Control-Applicative.html#v:pure" title="Control.Applicative" class="">pure</a></code> ()
guard False = <code class=""><a href="https://hackage.haskell.org/package/base-4.14.0.0/docs/Control-Applicative.html#v:empty" title="Control.Applicative" class="">empty</a><br class=""></code></pre><pre class=""><code class=""><font face="arial,sans-serif" class="">That means you can use the same thing in your own code<br class=""><br class="">import Control.Applicative<br class=""><br class="">pairs xs =<br class="">  xs >>= \x -><br class="">    xs >>= \y -><br class="">      if (x + y == 2020) then pure (x,y) else empty<br class=""></font></code></pre></div></div><br class=""><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Dec 2, 2020 at 5:31 AM mike h <<a href="mailto:mike_k_houghton@yahoo.co.uk" class="">mike_k_houghton@yahoo.co.uk</a>> wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="overflow-wrap: break-word;" class="">Hi,<div class=""> I have </div><div class=""><div style="color:rgb(51,51,51);background-color:rgb(253,246,227);font-family:Menlo,Monaco,"Courier New",monospace;font-size:20px;line-height:30px;white-space:pre-wrap" class=""><div class="">sumIs2020P1' xs <span style="color:rgb(133,153,0)" class="">=</span> <span style="color:rgb(133,153,0)" class="">do</span> </div><div class="">    x <span style="color:rgb(133,153,0)" class=""><-</span> xs</div><div class="">    y <span style="color:rgb(133,153,0)" class=""><-</span> xs</div><div class="">    guard (x <span style="color:rgb(133,153,0)" class="">+</span> y <span style="color:rgb(133,153,0)" class="">==</span> <span style="color:rgb(211,54,130)" class="">2020</span>)</div><div class="">    pure (x,y)</div></div></div><div class=""><br class=""></div><div class="">which has been desugared from a list comprehension</div><div class="">I would like to reduce this even more using >>= </div><div class="">So I do</div><div class=""><div style="color:rgb(51,51,51);background-color:rgb(253,246,227);font-family:Menlo,Monaco,"Courier New",monospace;font-size:20px;line-height:30px;white-space:pre-wrap" class=""><div class="">sumIs2020P1'' xs <span style="color:rgb(133,153,0)" class="">=</span>  (a,b) <span style="color:rgb(133,153,0)" class="">where</span></div><div class="">    (a,b)<span style="color:rgb(133,153,0)" class="">:</span>rest <span style="color:rgb(133,153,0)" class="">=</span> filter (<span style="color:rgb(133,153,0)" class="">\</span>(x,y) <span style="color:rgb(133,153,0)" class="">-></span> x <span style="color:rgb(133,153,0)" class="">+</span> y <span style="color:rgb(133,153,0)" class="">==</span> <span style="color:rgb(211,54,130)" class="">2020</span>)  pairs       </div><br class=""><div class="">    pairs <span style="color:rgb(133,153,0)" class="">=</span> xs <span style="color:rgb(133,153,0)" class="">>>=</span> <span style="color:rgb(133,153,0)" class="">\</span>x <span style="color:rgb(133,153,0)" class="">-></span> </div><div class="">                    xs <span style="color:rgb(133,153,0)" class="">>>=</span> <span style="color:rgb(133,153,0)" class="">\</span>y <span style="color:rgb(133,153,0)" class="">-></span> </div><div class="">                        pure (x,y)</div><div class=""></div></div></div><div class=""><br class=""></div><div class="">but really I would like the guard to be within the >>= sections but I could not work out </div><div class="">how to do it!</div><div class="">i.e. I’m looking for something like (pseudo code)</div><div class=""><br class=""></div><div class=""><div style="color:rgb(51,51,51);font-family:Menlo,Monaco,"Courier New",monospace;font-size:20px;white-space:pre-wrap;background-color:rgb(253,246,227)" class="">pairs <span style="color:rgb(133,153,0)" class="">=</span> xs <span style="color:rgb(133,153,0)" class="">>>=</span> <span style="color:rgb(133,153,0)" class="">\</span>x <span style="color:rgb(133,153,0)" class="">-></span> </div><div style="color:rgb(51,51,51);font-family:Menlo,Monaco,"Courier New",monospace;font-size:20px;white-space:pre-wrap;background-color:rgb(253,246,227)" class="">                    xs <span style="color:rgb(133,153,0)" class="">>>=</span> <span style="color:rgb(133,153,0)" class="">\</span>y <span style="color:rgb(133,153,0)" class="">-></span> </div><div style="color:rgb(51,51,51);font-family:Menlo,Monaco,"Courier New",monospace;font-size:20px;white-space:pre-wrap;background-color:rgb(253,246,227)" class=""><span style="white-space:pre-wrap" class="">                       </span>if (x + y == 2020) then pure (x,y) else DO_NOTHING</div><div style="color:rgb(51,51,51);font-family:Menlo,Monaco,"Courier New",monospace;font-size:20px;white-space:pre-wrap;background-color:rgb(253,246,227)" class="">                        </div></div><div class="">which would then allow the filter to be removed. </div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Many Thanks</div><div class=""><br class=""></div><div class="">Mike</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""><div class="">
<div class="">Dr Mike Houghton</div><div class=""><br class=""></div><div class=""><a href="mailto:mike_k_houghton@yahoo.co.uk" target="_blank" class="">mike_k_houghton@yahoo.co.uk</a></div><div class=""><br class=""></div><br class="">

</div>
<br class=""></div></div>_______________________________________________<br class="">
Beginners mailing list<br class="">
<a href="mailto:Beginners@haskell.org" target="_blank" class="">Beginners@haskell.org</a><br class="">
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank" class="">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br class="">
</blockquote></div>
_______________________________________________<br class="">Beginners mailing list<br class=""><a href="mailto:Beginners@haskell.org" class="">Beginners@haskell.org</a><br class="">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners<br class=""></div></blockquote></div><br class=""></body></html>