Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is a lot to say in answer to your question, however, since you asked, I will offer this "rule of thumb."</p> <p>If you are using <code>do</code>-notation and your generated values[1] are not used in the expressions that you are sequencing[2], then that code can transform to an Applicative style. Similarly, if you use one or more of the generated values in an expression that is sequenced, then you must use <code>Monad</code> and <code>Applicative</code> is not strong enough to achieve the same code.</p> <p>For example, let us look at the following code:</p> <pre><code>do a &lt;- e1 b &lt;- e2 c &lt;- e3 return (f a b c) </code></pre> <p>We see that in none of the expressions to the right of <code>&lt;-</code> do any of the generated values (<code>a</code>, <code>b</code>, <code>c</code>) appear. Therefore, we can transform it to using Applicative code. Here is one possible transformation:</p> <pre><code>f &lt;$&gt; e1 &lt;*&gt; e2 &lt;*&gt; e3 </code></pre> <p>and another:</p> <pre><code>liftA3 f e1 e2 e3 </code></pre> <p>On the other hand, take this piece of code for example:</p> <pre><code>do a &lt;- e1 b &lt;- e2 a c &lt;- e3 return (f b c) </code></pre> <p>This code cannot use <code>Applicative</code>[3] because the generated value <code>a</code> is used later in an expression in the comprehension. This must use <code>Monad</code> to get to its result -- attempt to factor it into <code>Applicative</code> to get a feel for why.</p> <p>There are some further interesting and useful details on this subject, however, I just intended to give you this rule of thumb whereby you can skim over a <code>do</code>-comprehension and determine pretty quickly if it can be factored into <code>Applicative</code> style code.</p> <p>[1] Those that appear to the left of <code>&lt;-</code>.</p> <p>[2] Expressions that appear to the right of <code>&lt;-</code>.</p> <p>[3] strictly speaking, parts of it could, by factoring out <code>e2 a</code>.</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. 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