Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is an excellent use case for <a href="http://www.haskell.org/haskellwiki/Applicative_functor#Usage" rel="noreferrer" title="HaskellWiki/Applicative Functors">applicative style</a>. You can replace your entire snippet (after importing <code>Control.Applicative</code>) with</p> <pre><code>Node &lt;$&gt; numberNode x &lt;*&gt; numberTree t1 &lt;*&gt; numberTree t2 </code></pre> <p>Think of the applicative style (using <code>&lt;$&gt;</code> and <code>&lt;*&gt;</code>) as "lifting" function application so it works on functors as well. If you mentally ignore <code>&lt;$&gt;</code> and <code>&lt;*&gt;</code> it looks quite a lot like normal function application!</p> <p>Applicative style is useful whenever you have a pure function and you want to give it impure arguments (or any functor arguments, really) -- basically when you want to do what you specified in your question!</p> <hr> <p>The type signature of <code>&lt;$&gt;</code> is</p> <pre><code>(&lt;$&gt;) :: Functor f =&gt; (a -&gt; b) -&gt; f a -&gt; f b </code></pre> <p>which means it takes a pure function (in this case <code>Node</code>) and a functor value (in this case <code>numberNode x</code>) and it creates a new function wrapped "inside" a functor. You can add further arguments to this function with <code>&lt;*&gt;</code>, which has the type signature</p> <pre><code>(&lt;*&gt;) :: Applicative f =&gt; f (a -&gt; b) -&gt; f a -&gt; f b </code></pre> <p>As you can see, this is very similar to <code>&lt;$&gt;</code> only it works even when the function is wrapped "inside" a functor.</p>
    singulars
    1. This table or related slice is empty.
    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. 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