Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A monad in Haskell is something that has two operations defined:</p> <pre><code>(&gt;&gt;=) :: Monad m =&gt; m a -&gt; (a -&gt; m b) -&gt; m b -- also called bind return :: Monad m =&gt; a -&gt; m a </code></pre> <p>These two operations need to satisfy certain laws that really might just confuse you at this point, if you don't have a knack for mathy ideas. Conceptually, you use bind to operate on values on a monadic level and return to create monadic values from "trivial" ones. For instance,</p> <p><code>getLine :: IO String</code>, </p> <p>so you cannot modify and <code>putStrLn</code> this <code>String</code> -- because it's not a <code>String</code> but an <code>IO String</code>!</p> <p>Well, we have an IO Monad handy, so not to worry. All we have to do is use bind to do what we want. Let's see what bind looks like in the IO Monad:</p> <pre><code>(&gt;&gt;=) :: IO a -&gt; (a -&gt; IO b) -&gt; IO b </code></pre> <p>And if we place <code>getLine</code> at the left hand side of bind, we can make it more specific yet.</p> <pre><code>(&gt;&gt;=) :: IO String -&gt; (String -&gt; IO b) -&gt; IO b </code></pre> <p>Okay, so <code>getLine &gt;&gt;= putStrLn . (++ ". No problem after all!")</code> would print the entered line with the extra content added. The right hand side is a function that takes a <code>String</code> and produces an <code>IO ()</code> - that wasn't hard at all! We just go by the types.</p> <p>There are Monads defined for a lot of different types, for instance <code>Maybe</code> and <code>[a]</code>, and they behave conceptually in the same way.</p> <p><code>Just 2 &gt;&gt;= return . (+2)</code> would yield <code>Just 4</code>, as you might expect. Note that we had to use <code>return</code> here, because otherwise the function on the right hand side would <strong>not</strong> match the return type <code>m b</code>, but just <code>b</code>, which would be a type error. It worked in the case of putStrLn because it already produces an <code>IO</code> something, which was exactly what our type needed to match. (Spoiler: Expressions of shape <code>foo &gt;&gt;= return . bar</code> are silly, because every <code>Monad</code> is a <code>Functor</code>. Can you figure out what that means?)</p> <p>I personally think that this is as far as intuition will get you on the topic of monads, and if you want to dive deeper, you really do need to dive into the theory. I liked getting a hang of just using them first. You can look up the source for various Monad instances, for instance the List (<code>[]</code>) Monad or <code>Maybe</code> Monad on Hoogle and get a bit smarter on the exact implementations. Once you feel comfortable with that, have a go at the actual monad laws and try to gain a more theoretical understanding for them!</p>
 

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