|
32 | 32 | </ul> |
33 | 33 | </div> |
34 | 34 | <h1 id="for-a-few-monads-more">For a Few Monads More</h1> |
35 | | -<p><img src="assets/images/for-a-few-monads-more/clint.png" |
| 35 | +<img src="assets/images/for-a-few-monads-more/clint.png" |
36 | 36 | class="right" width="189" height="400" |
37 | | -alt="there are two kinds of people in the world, my friend. those who learn them a haskell and those who have the job of coding java" /></p> |
| 37 | +alt="there are two kinds of people in the world, my friend. those who learn them a haskell and those who have the job of coding java"> |
38 | 38 | <p>We’ve seen how monads can be used to take values with contexts and |
39 | 39 | apply them to functions and how using <code>>>=</code> or |
40 | 40 | <code>do</code> notation allows us to focus on the values themselves |
@@ -86,9 +86,9 @@ <h2 id="writer">Writer? I hardly know her!</h2> |
86 | 86 | (False,"Compared gang size to 9.") |
87 | 87 | ghci> isBigGang 30 |
88 | 88 | (True,"Compared gang size to 9.")</code></pre> |
89 | | -<p><img src="assets/images/for-a-few-monads-more/tuco.png" class="left" |
| 89 | +<img src="assets/images/for-a-few-monads-more/tuco.png" class="left" |
90 | 90 | width="196" height="280" |
91 | | -alt="when you have to poop, poop, don’t talk" /></p> |
| 91 | +alt="when you have to poop, poop, don’t talk"> |
92 | 92 | <p>So far so good. <code>isBigGang</code> takes a normal value and |
93 | 93 | returns a value with a context. As we’ve just seen, feeding it a normal |
94 | 94 | value is not a problem. Now what if we already have a value that has a |
@@ -255,9 +255,9 @@ <h3 id="the-writer-type">The Writer type</h3> |
255 | 255 | <pre class="haskell:hs"><code>instance (Monoid w) => Monad (Writer w) where |
256 | 256 | return x = Writer (x, mempty) |
257 | 257 | (Writer (x,v)) >>= f = let (Writer (y, v')) = f x in Writer (y, v `mappend` v')</code></pre> |
258 | | -<p><img src="assets/images/for-a-few-monads-more/angeleyes.png" |
| 258 | +<img src="assets/images/for-a-few-monads-more/angeleyes.png" |
259 | 259 | class="right" width="383" height="248" |
260 | | -alt="when you have to poop, poop, don’t talk" /></p> |
| 260 | +alt="when you have to poop, poop, don’t talk"> |
261 | 261 | <p>First off, let’s examine <code>>>=</code>. Its implementation |
262 | 262 | is essentially the same as <code>applyLog</code>, only now that our |
263 | 263 | tuple is wrapped in the <code>Writer</code> <code>newtype</code>, we |
@@ -492,8 +492,8 @@ <h3 id="inefficient-list-construction">Inefficient list |
492 | 492 | <p>It’s inefficient because it ends up associating the use of |
493 | 493 | <code>++</code> to the left instead of to the right.</p> |
494 | 494 | <h3 id="difference-lists">Difference lists</h3> |
495 | | -<p><img src="assets/images/for-a-few-monads-more/cactus.png" |
496 | | -class="left" width="147" height="300" alt="cactuses" /></p> |
| 495 | +<img src="assets/images/for-a-few-monads-more/cactus.png" |
| 496 | +class="left" width="147" height="300" alt="cactuses"> |
497 | 497 | <p>Because lists can sometimes be inefficient when repeatedly appended |
498 | 498 | in this manner, it’s best to use a data structure that always supports |
499 | 499 | efficient appending. One such data structure is the difference list. A |
@@ -617,8 +617,8 @@ <h3 id="comparing-performance">Comparing Performance</h3> |
617 | 617 | <p>Oh, by the way, the song Final Countdown by Europe is now stuck in |
618 | 618 | your head. Enjoy!</p> |
619 | 619 | <h2 id="reader">Reader? Ugh, not this joke again.</h2> |
620 | | -<p><img src="assets/images/for-a-few-monads-more/revolver.png" |
621 | | -class="left" width="280" height="106" alt="bang youre dead" /></p> |
| 620 | +<img src="assets/images/for-a-few-monads-more/revolver.png" |
| 621 | +class="left" width="280" height="106" alt="bang youre dead"> |
622 | 622 | <p>In the <a |
623 | 623 | href="functors-applicative-functors-and-monoids.html">chapter about |
624 | 624 | applicatives</a>, we saw that the function type, <code>(->) r</code> |
@@ -725,8 +725,8 @@ <h2 id="reader">Reader? Ugh, not this joke again.</h2> |
725 | 725 | results and the <code>>>=</code> implementation will make sure |
726 | 726 | that it all works out.</p> |
727 | 727 | <h2 id="state">Tasteful stateful computations</h2> |
728 | | -<p><img src="assets/images/for-a-few-monads-more/texas.png" class="left" |
729 | | -width="244" height="230" alt="don’t jest with texas" /></p> |
| 728 | +<img src="assets/images/for-a-few-monads-more/texas.png" class="left" |
| 729 | +width="244" height="230" alt="don’t jest with texas"> |
730 | 730 | <p>Haskell is a pure language and because of that, our programs are made |
731 | 731 | of functions that can’t change any global state or variables, they can |
732 | 732 | only do some computations and return them results. This restriction |
@@ -876,8 +876,8 @@ <h3 id="the-state-monad">The State monad</h3> |
876 | 876 | because <code>return</code> has to put a value in a minimal context. So |
877 | 877 | <code>return</code> will make a stateful computation that presents a |
878 | 878 | certain value as the result and keeps the state unchanged.</p> |
879 | | -<p><img src="assets/images/for-a-few-monads-more/badge.png" |
880 | | -class="right" width="182" height="160" alt="im a cop" /></p> |
| 879 | +<img src="assets/images/for-a-few-monads-more/badge.png" |
| 880 | +class="right" width="182" height="160" alt="im a cop"> |
881 | 881 | <p>What about <code>>>=</code>? Well, the result of feeding a |
882 | 882 | stateful computation to a function with <code>>>=</code> has to be |
883 | 883 | a stateful computation, right? So we start off with the |
@@ -1146,8 +1146,8 @@ <h2 id="useful-monadic-functions">Some useful monadic functions</h2> |
1146 | 1146 | counterparts of functions that we already know, like <code>filter</code> |
1147 | 1147 | and <code>foldl</code>. Let’s see what they are then!</p> |
1148 | 1148 | <h3 id="liftm-and-friends">liftM and friends</h3> |
1149 | | -<p><img src="assets/images/for-a-few-monads-more/wolf.png" class="right" |
1150 | | -width="394" height="222" alt="im a cop too" /></p> |
| 1149 | +<img src="assets/images/for-a-few-monads-more/wolf.png" class="right" |
| 1150 | +width="394" height="222" alt="im a cop too"> |
1151 | 1151 | <p>When we started our journey to the top of Monad Mountain, we first |
1152 | 1152 | looked at functors, which are for things that can be mapped over. Then, |
1153 | 1153 | we learned about improved functors called applicative functors, which |
@@ -1363,8 +1363,8 @@ <h3 id="the-join-function">The join function</h3> |
1363 | 1363 | joinedMaybes = do |
1364 | 1364 | m <- Just (Just 8) |
1365 | 1365 | m</code></pre> |
1366 | | -<p><img src="assets/images/for-a-few-monads-more/tipi.png" class="right" |
1367 | | -width="253" height="379" alt="im a cop too as well also" /></p> |
| 1366 | +<img src="assets/images/for-a-few-monads-more/tipi.png" class="right" |
| 1367 | +width="253" height="379" alt="im a cop too as well also"> |
1368 | 1368 | <p>Perhaps the most interesting thing about <code>join</code> is that |
1369 | 1369 | for every monad, feeding a monadic value to a function with |
1370 | 1370 | <code>>>=</code> is the same thing as just mapping that function |
@@ -1532,8 +1532,8 @@ <h3 id="foldm">foldM</h3> |
1532 | 1532 | is cool as well because then you log whatever you want as your fold goes |
1533 | 1533 | along its way.</p> |
1534 | 1534 | <h3 id="making-a-safe-rpn-calculator">Making a safe RPN calculator</h3> |
1535 | | -<p><img src="assets/images/for-a-few-monads-more/miner.png" class="left" |
1536 | | -width="280" height="396" alt="i’ve found yellow!" /></p> |
| 1535 | +<img src="assets/images/for-a-few-monads-more/miner.png" class="left" |
| 1536 | +width="280" height="396" alt="i’ve found yellow!"> |
1537 | 1537 | <p>When we were solving the problem of <a |
1538 | 1538 | href="http://learnyouahaskell.com/reverse-polish-notation-calculator">implementing |
1539 | 1539 | a RPN calculator</a>, we noted that it worked fine as long as the input |
@@ -1717,8 +1717,8 @@ <h3 id="composing-monadic-functions">Composing monadic functions</h3> |
1717 | 1717 | <pre class="haskell:hs"><code>canReachIn :: Int -> KnightPos -> KnightPos -> Bool |
1718 | 1718 | canReachIn x start end = end `elem` inMany x start</code></pre> |
1719 | 1719 | <h2 id="making-monads">Making monads</h2> |
1720 | | -<p><img src="assets/images/for-a-few-monads-more/spearhead.png" |
1721 | | -class="center" width="780" height="244" alt="kewl" /></p> |
| 1720 | +<img src="assets/images/for-a-few-monads-more/spearhead.png" |
| 1721 | +class="center" width="780" height="244" alt="kewl"> |
1722 | 1722 | <p>In this section, we’re going to look at an example of how a type gets |
1723 | 1723 | made, identified as a monad and then given the appropriate |
1724 | 1724 | <code>Monad</code> instance. We don’t usually set out to make a monad |
@@ -1817,8 +1817,8 @@ <h2 id="making-monads">Making monads</h2> |
1817 | 1817 | happen. <code>'c'</code> and <code>'d'</code> are also equally likely to |
1818 | 1818 | happen. Here’s a picture of a probability list that models this |
1819 | 1819 | scenario:</p> |
1820 | | -<p><img src="assets/images/for-a-few-monads-more/prob.png" class="left" |
1821 | | -width="456" height="142" alt="probs" /></p> |
| 1820 | +<img src="assets/images/for-a-few-monads-more/prob.png" class="left" |
| 1821 | +width="456" height="142" alt="probs"> |
1822 | 1822 | <p>What are the chances for each of these letters to occur? If we were |
1823 | 1823 | to draw this as just four boxes, each with a probability, what would |
1824 | 1824 | those probabilities be? To find out, all we have to do is multiply each |
@@ -1856,8 +1856,8 @@ <h2 id="making-monads">Making monads</h2> |
1856 | 1856 | return x = Prob [(x,1%1)] |
1857 | 1857 | m >>= f = flatten (fmap f m) |
1858 | 1858 | fail _ = Prob []</code></pre> |
1859 | | -<p><img src="assets/images/for-a-few-monads-more/ride.png" class="right" |
1860 | | -width="177" height="406" alt="ride em cowboy" /></p> |
| 1859 | +<img src="assets/images/for-a-few-monads-more/ride.png" class="right" |
| 1860 | +width="177" height="406" alt="ride em cowboy"> |
1861 | 1861 | <p>Because we already did all the hard work, the instance is very |
1862 | 1862 | simple. We also defined the <code>fail</code> function, which is the |
1863 | 1863 | same as it is for lists, so if there’s a pattern match failure in a |
|
0 commit comments