Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Actually, contrary to common understanding of Monads, they have nothing to do with state. Monads are simply a way to wrapping things and provide methods to do operations on the wrapped stuff without unwrapping it.</p> <p>For example, you can create a type to wrap another one, in Haskell:</p> <pre><code>data Wrapped a = Wrap a </code></pre> <p>To wrap stuff we define</p> <pre><code>return :: a -&gt; Wrapped a return x = Wrap x </code></pre> <p>To perform operations without unwrapping, say you have a function <code>f :: a -&gt; b</code>, then you can do this to <em>lift</em> that function to act on wrapped values:</p> <pre><code>fmap :: (a -&gt; b) -&gt; (Wrapped a -&gt; Wrapped b) fmap f (Wrap x) = Wrap (f x) </code></pre> <p>That's about all there is to understand. However, it turns out that there is a more general function to do this <em>lifting</em>, which is <code>bind</code>:</p> <pre><code>bind :: (a -&gt; Wrapped b) -&gt; (Wrapped a -&gt; Wrapped b) bind f (Wrap x) = f x </code></pre> <p><code>bind</code> can do a bit more than <code>fmap</code>, but not vice versa. Actually, <code>fmap</code> can be defined only in terms of <code>bind</code> and <code>return</code>. So, when defining a monad.. you give its type (here it was <code>Wrapped a</code>) and then say how its <code>return</code> and <code>bind</code> operations work.</p> <p>The cool thing is that this turns out to be such a general pattern that it pops up all over the place, encapsulating state in a pure way is only one of them.</p> <p>For a good article on how monads can be used to introduce functional dependencies and thus control order of evaluation, like it is used in Haskell's IO monad, check out <a href="http://www.haskell.org/haskellwiki/IO_inside" rel="noreferrer">IO Inside</a>.</p> <p>As for understanding monads, don't worry too much about it. Read about them what you find interesting and don't worry if you don't understand right away. Then just diving in a language like Haskell is the way to go. Monads are one of these things where understanding trickles into your brain by practice, one day you just suddenly realize you understand them.</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