Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Regarding monads:</p> <p>Yes, <code>Either a</code> is a monad. So simplifying the problem, you are basically asking for this:</p> <pre><code>main = print $ magicMonadUnwrap v v :: Either String Int v = Right 3 magicMonadUnwrap :: (Monad m) =&gt; m a -&gt; a magicMonadUnwrap = undefined </code></pre> <p>How do you define <code>magicMonadUnwrap</code>? Well, you see, it's different for each monad. Each one needs its own unwrapper. Many of these have the word "run" in them, for example, <code>runST</code>, <code>runCont</code>, or <code>runEval</code>. However, for some monads, it might not be safe to unwrap them (hence the need for differing unwrappers).</p> <p>One implementation for lists would be <code>head</code>. But what if the list is empty? An unwrapper for <code>Maybe</code> is <code>fromJust</code>, but what if it's <code>Nothing</code>?</p> <p>Similarly, the unwrapper for the <code>Either</code> monad would be something like:</p> <pre><code>fromRight :: Either a b -&gt; b fromRight (Right x) = x </code></pre> <p>But this unwrapper isn't safe: what if you had a <code>Left</code> value instead? (Left usually represents an error state, in your case, a parse error). So the best way to act upon an <code>Either</code> value it is to use the <code>either</code> function, or else use a case statement matching <code>Right</code> and <code>Left</code>, as Daniel Wagner illustrated.</p> <p><em><strong>tl;dr</em></strong>: there is no <code>magicMonadUnwrap</code>. If you're inside that same monad, you can use <code>&lt;-</code>, but to truly extract the value from a monad...well...how you do it depends on which monad you're dealing with.</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