Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>do</code> notation in Haskell desugars in a pretty simple way.</p> <pre><code>do x &lt;- foo e1 e2 ... </code></pre> <p>turns into</p> <pre><code> foo &gt;&gt;= \x -&gt; do e1 e2 </code></pre> <p>and</p> <pre><code>do x e1 e2 ... </code></pre> <p>into</p> <pre><code>x &gt;&gt; do e1 e2 .... </code></pre> <p>This means you can really write any monadic computation with <code>&gt;&gt;=</code> and <code>return</code>. The only reason why we don't is because it's just more painful syntax. Monads are useful for imitating imperative code, <code>do</code> notation makes it look like it.</p> <p>The C-ish syntax makes it far easier for beginners to understand it. You're right it doesn't look as functional, but requiring someone to grok monads properly before they can use IO is a pretty big deterrent.</p> <p>The reason why we'd use <code>&gt;&gt;=</code> and <code>return</code> on the other hand is because it's <em>much</em> more compact for 1 - 2 liners. However it does tend to get a bit more unreadable for anything too big. So to directly answer your question, No please don't avoid do notation when appropriate.</p> <p>Lastly the two operators you saw, <code>&lt;$&gt;</code> and <code>&lt;*&gt;</code>, are actually fmap and applicative respectively, not monadic. They can't actually be used to represent a lot of what do notation does. They're more compact to be sure, but they don't let you easily name intermediate values. Personally, I use them about 80% of the time, mostly because I tend to write very small composable functions anyways which applicatives are great for.</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.
    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