Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, that style is actually rather idiomatic. Since you're doing two things to two different values, there is some irreducible complexity; the actual pattern match does not introduce much on its own. Besides, I personally find the explicit style very readable most of the time.</p> <p>However, there is an alternative. <code>Control.Arrow</code> has a bunch of functions for working with tuples. Since the function arrow <code>-&gt;</code> is an <code>Arrow</code> as well, all these work for normal functions. </p> <p>So you could rewrite your second example using <code>(***)</code> to combine two functions to work over tuples. This operator has the following type:</p> <pre><code>(***) :: a b c -&gt; a b' c' -&gt; a (b, b') (c, c') </code></pre> <p>If we replace <code>a</code> with <code>-&gt;</code>, we get:</p> <pre><code>(***) :: (b -&gt; c) -&gt; (b' -&gt; c') -&gt; ((b, b') -&gt; (c, c')) </code></pre> <p>So you could combine <code>(+ x)</code> and <code>(- x)</code> into a single function with <code>(+ x) *** (- x)</code>. This would be equivalent to:</p> <pre><code>\ (a, b) -&gt; (a + x, b - x) </code></pre> <p>Then you could use it in your recursion. Unfortunately, the <code>-</code> operator is stupid and doesn't work in sections, so you would have to write it with a lambda:</p> <pre><code>(+ x) *** (\ a -&gt; a - x) $ go xs </code></pre> <p>You can obviously imagine using any other operator, all of which <em>aren't</em> quite as stupid :).</p> <p>Honestly, I think this version is less readable than the original. However, in other cases, the <code>***</code> version <em>can</em> be more readable, so it's useful to know about it. In particular, if you were passing <code>(+ x) *** (- x)</code> into a higher-order function instead of applying it immediately, I think the <code>***</code> version would be better than an explicit lambda.</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. 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