<div class="gmail_extra"><div class="gmail_quote">On Wed, Oct 31, 2012 at 10:44 PM, Daniel Trstenjak <span dir="ltr">&lt;<a href="mailto:daniel.trstenjak@gmail.com" target="_blank">daniel.trstenjak@gmail.com</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
(&lt;||&gt;) :: Monad m =&gt; m Bool -&gt; m Bool -&gt; m Bool<br>
(&lt;||&gt;) m1 m2 = do<br>
   r1 &lt;- m1<br>
   if r1 then return True else m2<br>
<br></blockquote><div><br>We can do some algebraic simplification, using the law<br><i><b>if P  then True else Q </b> </i>is same as <b>P || Q</b> :<br><br>(&lt;||&gt;) m1 m2 = do<br>   r1 &lt;- m1<br>   r2 &lt;- m2<br>   return (r1 || r2)<br>
<br>Which reminds me of the definition of liftM2<br>(from Control.Monad):<br><pre><a name="line-288"></a><span class="hs-definition">liftM2</span> <span class="hs-varid">f</span> <span class="hs-varid">m1</span> <span class="hs-varid">m2</span>          <span class="hs-keyglyph">=</span> <span class="hs-keyword">do</span> <span class="hs-layout">{</span> <span class="hs-varid">x1</span> <span class="hs-keyglyph">&lt;-</span> <span class="hs-varid">m1</span><span class="hs-layout">;</span> <span class="hs-varid">x2</span> <span class="hs-keyglyph">&lt;-</span> <span class="hs-varid">m2</span><span class="hs-layout">;</span> <span class="hs-varid">return</span> <span class="hs-layout">(</span><span class="hs-varid">f</span> <span class="hs-varid">x1</span> <span class="hs-varid">x2</span><span class="hs-layout">)</span> <span class="hs-layout">}</span> 
<a name="line-289"></a></pre> And so we can golf down to:<br>(&lt;||&gt;) = liftM2 (||)<br><br>Now, instead of using liftM2 from Control.Monad, I could as well use liftA2 from Control.Applicative :<br><br>(&lt;||&gt;) = liftA2 (||)<br>
<br>So my questions to the experts: <br><br>1&gt; Are these equivalent or am I missing something?<br>2&gt; Any thumb rules on when to use Control.Monad and when to use Control.Applicative?<br><br>Rusi <br></div></div>-- <br>
<a href="http://blog.languager.org" target="_blank">http://blog.languager.org</a><br>
<br><br>
</div>