Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>&lt;$&gt;</code> (aka <code>fmap</code>) is a member of the Functor class like so:</p> <pre><code>class Functor f where fmap :: (a -&gt; b) -&gt; f a -&gt; f b </code></pre> <p>So whatever f is must be a parameterised type with one type argument. Lists are one such type, when written in their prefix form <code>[]</code> (<code>[] a</code> is the same as <code>[a]</code>). So the instance for lists is:</p> <pre><code>instance Functor [] where -- fmap :: (a -&gt; b) -&gt; [] a -&gt; [] b fmap = map </code></pre> <p>Pairs can also be written in prefix form: <code>(,) a b</code> is the same as <code>(a, b)</code>. So let's consider what we do if we want a Functor instance involving pairs. We <strong>can't</strong> declare an <code>instance Functor (,)</code> because the pair constructor <code>(,)</code> takes two types -- and they can be different types! What we can do is declare an instance for <code>(,) a</code> -- that's a type that only needs one more type:</p> <pre><code>instance Functor ( (,) a ) where -- fmap :: (b -&gt; c) -&gt; (,) a b -&gt; (,) a c fmap f (x, y) = (x, f y) </code></pre> <p>Hopefully you can see that the definition of fmap is the only sensible one we can give. The answer as to why the functor instance operates on the second item in a pair is that the type for the second item comes last in the list! We can't easily declare a functor instance that operates on the first item in a pair. Incidentally, this generalises to larger tuples, e.g. the quadruple <code>(,,,) a b c d</code> (aka <code>(a, b, c, d)</code>) can also have a <code>Functor</code> instance on the last item:</p> <pre><code>instance Functor ( (,,,) a b c) where -- fmap :: (d -&gt; e) -&gt; (,,,) a b c d -&gt; (,,,) a b c e fmap f (p, q, r, s) = (p, q, r, f s) </code></pre> <p>Hope that helps explain it all!</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