Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, remember how <code>fmap</code> is defined for applicatives:</p> <pre><code>fmap f x = pure f &lt;*&gt; x </code></pre> <p>This means that your example is the same as <code>(fmap (+ 5) (* 3)) 4</code>. The <code>fmap</code> function for functions is just composition, so your exact expression is the same as <code>((+ 5) . (* 3)) 4</code>.</p> <p>Now, let's think about why the instance is written the way it is. What <code>&lt;*&gt;</code> does is essentially apply a function in the functor to a value in the functor. Specializing to <code>(-&gt;) r</code>, this means it applies a function returned by a function from <code>r</code> to a value returned by a function from <code>r</code>. A function that returns a function is just a function of two arguments. So the real question is this: how would you apply a function of two arguments (<code>r</code> and <code>a</code>, returning <code>b</code>) to a value <code>a</code> returned by a function from <code>r</code>?</p> <p>The first thing to note is that you have to return a value of type <code>(-&gt;) r</code> which means the result also has to be a function from <code>r</code>. For reference, here is the <code>&lt;*&gt;</code> function:</p> <pre><code>f &lt;*&gt; g = \x -&gt; f x (g x) </code></pre> <p>Since we want to return a function taking a value of type <code>r</code>, <code>x :: r</code>. The function we return has to have a type <code>r -&gt; b</code>. How can we get a value of type <code>b</code>? Well, we have a function <code>f :: r -&gt; a -&gt; b</code>. Since <code>r</code> is going to be the argument of the result function, we get that for free. So now we have a function from <code>a -&gt; b</code>. So, as long as we have some value of type <code>a</code>, we can get a value of type <code>b</code>. But how do we get a value of type <code>a</code>? Well, we have another function <code>g :: r -&gt; a</code>. So we can take our value of type <code>r</code> (the parameter <code>x</code>) and use it to get a value of type <code>a</code>. </p> <p>So the final idea is simple: we use the parameter to first get a value of type <code>a</code> by plugging it into <code>g</code>. The parameter has type <code>r</code>, <code>g</code> has type <code>r -&gt; a</code>, so we have an <code>a</code>. Then, we plug both the parameter and the new value into <code>f</code>. We need both because <code>f</code> has a type <code>r -&gt; a -&gt; b</code>. Once we plug both an <code>r</code> and an <code>a</code> in, we have a <code>b1</code>. Since the parameter is in a lambda, the result has a type <code>r -&gt; b</code>, which is what we want.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload