Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>My preferred implementation for this is</p> <pre><code>fmap . fmap :: (Functor f, Functor f1) =&gt; (a -&gt; b) -&gt; f (f1 a) -&gt; f (f1 b) </code></pre> <p>If only because it is fairly easy to remember.</p> <p>When instantiating f and f1 to <code>(-&gt;) c</code> and <code>(-&gt;) d</code> respectively you get the type</p> <pre><code>(a -&gt; b) -&gt; (c -&gt; d -&gt; a) -&gt; c -&gt; d -&gt; b </code></pre> <p>which is the type of </p> <pre><code>(.) . (.) :: (b -&gt; c) -&gt; (a -&gt; a1 -&gt; b) -&gt; a -&gt; a1 -&gt; c </code></pre> <p>but it is a bit easier to rattle off the <code>fmap . fmap</code> version and it generalizes to other functors.</p> <p>Sometimes this is written <code>fmap fmap fmap</code>, but written as <code>fmap . fmap</code> it can be more readily expanded to allow more arguments.</p> <pre><code>fmap . fmap . fmap :: (Functor f, Functor g, Functor h) =&gt; (a -&gt; b) -&gt; f (g (h a)) -&gt; f (g (h b)) fmap . fmap . fmap . fmap :: (Functor f, Functor g, Functor h, Functor i) =&gt; (a -&gt; b) -&gt; f (g (h (i a))) -&gt; f (g (h (i b)) </code></pre> <p>etc.</p> <p>In general <code>fmap</code> composed with itself <em>n</em> times can be used to <code>fmap</code> <em>n</em> levels deep! </p> <p>And since functions form a <code>Functor</code>, this provides plumbing for <em>n</em> arguments.</p> <p>For more information, see Conal Elliott's <a href="http://conal.net/blog/posts/semantic-editor-combinators/">Semantic Editor Combinators</a>.</p>
 

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