<div dir="ltr"><div><div>Hi,<br><br></div>In order to wrap my head around Applicatives and Monads, I started doing the <br>exercises at <a href="http://blog.tmorris.net/posts/20-intermediate-haskell-exercises/">http://blog.tmorris.net/posts/20-intermediate-haskell-exercises/</a> <br><br></div>The main class being used for the questions is:<br><div><pre class=""><code class=""><span class="">class</span> <span class="">Misty</span> m <span class="">where</span>
<span class="">  banana ::</span> (a <span class="">-></span> m b) <span class="">-></span> m a <span class="">-></span> m b
<span class="">  unicorn ::</span> a <span class="">-></span> m a<span class=""></span>
<span class="">  furry' ::</span> (a <span class="">-></span> b) <span class="">-></span> m a <span class="">-></span> m b
<span style="font-family:arial,helvetica,sans-serif"><br>Question #13 asks to create the following function:</span><br><br></code><code class=""><span class="">apple ::</span> (<span class="">Misty</span> m) <span class="">=></span> m a <span class="">-></span> m (a <span class="">-></span> b) <span class="">-></span> m b
<span class=""><br></span></code><code class=""><span class=""><code class=""><span class=""><span style="font-family:arial,helvetica,sans-serif">After thinking for around an hour for this, I could not crack it and tried <br>looking at other people's solutions, two of which are:</span><br></span></code><br><code class=""><span class="">apple <span class="">=</span> banana <span class="">.</span> <span class="">flip</span> furry'<br>apple ma mab = banana (\ab -> furry' ab ma) mab<br><br></span></code></span></code></pre><pre class=""><code class=""><span class=""></span></code></pre><pre class=""><code class=""><span class=""><font face="arial,helvetica,sans-serif">I also tried hoogle to see what this function is in the real world, and the <br>underlying implementation in the source code. Turns out, one of the <br>applicatives uses this flip technique (monads use a different one):<br><br></font></span></code><span class="">(<**>) = liftA2 (flip ($))</span><span></span><br></pre><pre class=""><code class=""><span class=""><span style="font-family:arial,helvetica,sans-serif">Although the code compiles with the solutions, I am still not able to wrap my<br>head around them. <br><br></span></span></code></pre><pre class=""><code class=""><span class=""><span style="font-family:arial,helvetica,sans-serif">For instance, from what I understand from composition, the output of one function <br>becomes the input of the next one. But the input and outputs of these functions vary:<br><br><span style="font-family:monospace,monospace">banana :: Misty m => (a -> m b) -> m a -> m b<br>flip furry' :: Misty m => m a -> (a -> b) -> m b<br><br>banana . flip furry' :: Misty m => m a -> m (a -> b) -> m b<br></span></span></span></code></pre><pre class=""><span style="font-family:arial,helvetica,sans-serif"><code class=""><span style="font-family:arial,helvetica,sans-serif">I am thinking that the entire <span style="font-family:monospace,monospace">`(a -> b) -> m b</span>` in <span style="font-family:monospace,monospace">flip furry</span>'</span> </code></span><code class=""><span style="font-family:arial,helvetica,sans-serif">is being sent to <span style="font-family:monospace,monospace">banana </span><br>as the first argument: <span style="font-family:monospace,monospace">(a -> m b)</span></span>, <span style="font-family:arial,helvetica,sans-serif">but what happens after that?</span><br><br></code></pre><pre class=""><code class=""><span style="font-family:arial,helvetica,sans-serif">In the second solution, the types pass, i.e. the lambda is basically <span style="font-family:monospace,monospace">furry'</span> which has the type<br><span style="font-family:monospace,monospace">a -> ... -> m b</span>, exactly what is needed in the call to <span style="font-family:monospace,monospace">banana</span>. But where will it get the <br>function <span style="font-family:monospace,monospace">ab</span>? And how will banana work with the second argument? The definition in the <br>class requires it to be <span style="font-family:monospace,monospace">`m a`</span>, but here we are passing it <span style="font-family:monospace,monospace">`m a -> m b`</span>.</span><br></code></pre>Sorry for such a long post, but I want to understand this wizardry . <br><br></div><div>Thanks,<br></div><div>Tushar<br></div></div>