<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;">Understood. Thanks.<br><br>A little further on in the tutorial they define a "parent" function.<br><br>The <code>mplus</code> operator is used to combine monadic values from
separate computations into a single monadic value.  Within the context
of our sheep-cloning example, we could use <code>Maybe</code>'s
<code>mplus</code> to define a function,
<code>parent&nbsp;s&nbsp;=&nbsp;(mother&nbsp;s)&nbsp;`mplus`&nbsp;(father&nbsp;s)</code>,
which would return a parent if there is one, and <code>Nothing</code> is the sheep has no parents at all.
For a sheep with both parents, the function would return one or the
other, depending on the exact definition of <code>mplus</code> in the
<code>Maybe</code> monad.<br><br>But I get this when I try to use it:<br><br>sheep.hs:30:22: Not in scope: `mplus'<br>[michael@localhost ~]$<br><br>And here's the sheep.hs file, attempting to use "parent" near the bottom<br><br>========= <br><br>{- Author:&nbsp;&nbsp;&nbsp;&nbsp; Jeff Newbern<br>&nbsp;&nbsp; Maintainer: Jeff Newbern &lt;jnewbern@nomaware.com&gt;<br>&nbsp;&nbsp; Time-stamp: &lt;Mon Nov 10 11:59:14 2003&gt;<br>&nbsp;&nbsp; License:&nbsp;&nbsp;&nbsp; GPL<br>-}<br><br>{- DESCRIPTION<br><br>Example 1 - Our first monad<br><br>Usage: Compile the code and execute the resulting program.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; It will print Dolly's maternal grandfather.<br>-}<br><br>-- everything you need to know about sheep<br>data Sheep = Sheep {name::String, mother::Maybe Sheep, father::Maybe Sheep}<br><br>-- we show sheep by name<br><br>instance Show Sheep where<br>&nbsp; show s = show (name s)<br><br>-- comb is a combinator for sequencing
 operations that return Maybe<br>comb :: Maybe a -&gt; (a -&gt; Maybe b) -&gt; Maybe b<br>comb Nothing&nbsp; _ = Nothing<br>comb (Just x) f = f x<br><br>parent s = (mother s) `mplus` (father s)<br><br>-- now we can use `comb` to build complicated sequences<br>maternalGrandfather :: Sheep -&gt; Maybe Sheep<br>maternalGrandfather s = (Just s) `comb` mother `comb` father<br><br>fathersMaternalGrandmother :: Sheep -&gt; Maybe Sheep<br>fathersMaternalGrandmother s = (Just s) `comb` father `comb` mother `comb` mother <br><br>mothersPaternalGrandfather :: Sheep -&gt; Maybe Sheep<br>mothersPaternalGrandfather s = (Just s) `comb` mother `comb` father `comb` father <br><br>-- this builds our sheep family tree<br>breedSheep :: Sheep<br>breedSheep = let adam&nbsp;&nbsp; = Sheep "Adam" Nothing Nothing<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; eve&nbsp;&nbsp;&nbsp; = Sheep "Eve" Nothing
 Nothing<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;uranus = Sheep "Uranus" Nothing Nothing<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;gaea&nbsp;&nbsp; = Sheep "Gaea" Nothing Nothing<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;kronos = Sheep "Kronos" (Just gaea) (Just uranus)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; holly&nbsp; = Sheep "Holly" (Just eve) (Just adam)<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; roger&nbsp; = Sheep "Roger" (Just eve) (Just kronos)<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; molly&nbsp; = Sheep "Molly" (Just holly) (Just roger)<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; in Sheep "Dolly" (Just molly) Nothing<br><br>-- print Dolly's maternal grandfather<br>main :: IO ()<br>main = let dolly = breedSheep<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; in do print (parent dolly)<br>&nbsp;&nbsp;&nbsp;
 &nbsp;&nbsp;&nbsp; <br>-- END OF FILE<br><br>Michael<br><br><br><br>--- On <b>Wed, 4/29/09, Anton van Straaten <i>&lt;anton@appsolutions.com&gt;</i></b> wrote:<br><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px;"><br>From: Anton van Straaten &lt;anton@appsolutions.com&gt;<br>Subject: Re: [Haskell-cafe] chr/ord?<br>To: "haskell-cafe@haskell.org" &lt;haskell-cafe@haskell.org&gt;<br>Date: Wednesday, April 29, 2009, 12:33 PM<br><br><div class="plainMail">michael rice wrote:<br>&gt; Since I'm trying to learn Monads, let's look at this as a teaching moment. The example code (see below), which I pulled off YAMT (Yet Another Monad Tutorial ;-)), is the source of my 'comb' function.<br>&gt; <br>&gt; I understand the code as it now stands, and I understand that the Prelude (&gt;&gt;=) would replace the 'comb'. Adding whatever statements are needed, how would you "specialize" the (&gt;&gt;=) to Maybe and solve this
 particular problem.<br><br>Saying that "comb is just (&gt;&gt;=) specialized to Maybe" just means that you can define comb like this:<br><br>&nbsp; comb :: Maybe a -&gt; (a -&gt; Maybe b) -&gt; Maybe b<br>&nbsp; comb = (&gt;&gt;=)<br><br>Which also of course means that you can typically use (&gt;&gt;=) instead of comb.&nbsp; Although in some cases, being more specific about the type can be useful.<br><br>You can do this sort of specialization for any polymorphic function, e.g.:<br><br>&nbsp; -- id is predefined in Haskell, definition given as example<br>&nbsp; id :: a -&gt; a<br>&nbsp; id x = x<br><br>&nbsp; intID :: Int -&gt; Int<br>&nbsp; intId = id<br><br>In that case, the compiler basically specializes the function for you, providing a version of it that's specific to Ints.<br><br>However, (&gt;&gt;=) is defined by the Monad type class, and as it happens there's also already a definition for it that's specific to the Maybe type.&nbsp; You can see
 GHC's source for it here:<br><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/src/Data-Maybe.html#Maybe" target="_blank">http://haskell.org/ghc/docs/latest/html/libraries/base/src/Data-Maybe.html#Maybe</a> <br><br>Not surprisingly, that definition is essentially identical to the definition of comb:<br><br>&nbsp; (Just x) &gt;&gt;= k&nbsp; &nbsp; &nbsp; = k x<br>&nbsp; Nothing&nbsp; &gt;&gt;= _&nbsp; &nbsp; &nbsp; = Nothing<br><br>So defining "comb = (&gt;&gt;=)" just uses that definition.<br><br>Anton<br><br>_______________________________________________<br>Haskell-Cafe mailing list<br><a ymailto="mailto:Haskell-Cafe@haskell.org" href="/mc/compose?to=Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br><a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br></div></blockquote></td></tr></table><br>