Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The following happens:</p> <p>You map fromMaybe over an IO value. Hence the left part</p> <pre><code>fromMaybe &lt;$&gt; (print "A" &gt;&gt; return True) </code></pre> <p>is an IO action that could be rewritten thus</p> <pre><code>print "A" &gt;&gt; return (fromMaybe True) :: IO (Maybe Bool -&gt; Bool) </code></pre> <p>This means that the "A" will be printed no matter what. </p> <p>Note that the IO monad is all about sequencing actions, hence an action later in the >>= chain can <strong>never</strong> affect whether earlier actions are executed.</p> <p>Consider</p> <pre><code>fromMaybe &lt;$&gt; (getChar &gt;&gt;= return) </code></pre> <p>It is clear that the Char the fromMaybe is applied to must come from actually reading a character. It is not the case that the character will only be read when it is needed.</p> <p>If this were so, the following code would not make sense:</p> <pre><code>do a &lt;- getChar b &lt;- getChar -- at this point, a and b have been actually read from stdin already return (a &lt; b) </code></pre> <p>For, it is not known whether (&lt;) evaluates its left or right argument first.</p> <p>Rather, in any case, a gets the value of the first character read and b that of the second. And the meaning of the code snippet is, accordingly, to read two characters and to check whether the first character read is lower than the second.</p> <p>Indeed, if an IO action would be executed only when its value is actually needed, many programs wouldn't print anything, as it stands. This is because code like</p> <pre><code> print "A" &gt;&gt; print "B" </code></pre> <p>deliberately ignores the result of the first print. </p> <p>For the same reason, the "B" will always be printed.</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