<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 s = (mother s) `mplus` (father 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: Jeff Newbern<br> Maintainer: Jeff Newbern <jnewbern@nomaware.com><br> Time-stamp: <Mon Nov 10 11:59:14 2003><br> License: GPL<br>-}<br><br>{- DESCRIPTION<br><br>Example 1 - Our first monad<br><br>Usage: Compile the code and execute the resulting program.<br> 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> show s = show (name s)<br><br>-- comb is a combinator for sequencing
operations that return Maybe<br>comb :: Maybe a -> (a -> Maybe b) -> Maybe b<br>comb Nothing _ = 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 -> Maybe Sheep<br>maternalGrandfather s = (Just s) `comb` mother `comb` father<br><br>fathersMaternalGrandmother :: Sheep -> Maybe Sheep<br>fathersMaternalGrandmother s = (Just s) `comb` father `comb` mother `comb` mother <br><br>mothersPaternalGrandfather :: Sheep -> 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 = Sheep "Adam" Nothing Nothing<br> eve = Sheep "Eve" Nothing
Nothing<br> uranus = Sheep "Uranus" Nothing Nothing<br> gaea = Sheep "Gaea" Nothing Nothing<br> kronos = Sheep "Kronos" (Just gaea) (Just uranus)<br> holly = Sheep "Holly" (Just eve) (Just adam)<br> roger = Sheep "Roger" (Just eve) (Just kronos)<br> molly = Sheep "Molly" (Just holly) (Just roger)<br> in Sheep "Dolly" (Just molly) Nothing<br><br>-- print Dolly's maternal grandfather<br>main :: IO ()<br>main = let dolly = breedSheep<br> in do print (parent dolly)<br>
<br>-- END OF FILE<br><br>Michael<br><br><br><br>--- On <b>Wed, 4/29/09, Anton van Straaten <i><anton@appsolutions.com></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 <anton@appsolutions.com><br>Subject: Re: [Haskell-cafe] chr/ord?<br>To: "haskell-cafe@haskell.org" <haskell-cafe@haskell.org><br>Date: Wednesday, April 29, 2009, 12:33 PM<br><br><div class="plainMail">michael rice wrote:<br>> 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>> <br>> I understand the code as it now stands, and I understand that the Prelude (>>=) would replace the 'comb'. Adding whatever statements are needed, how would you "specialize" the (>>=) to Maybe and solve this
particular problem.<br><br>Saying that "comb is just (>>=) specialized to Maybe" just means that you can define comb like this:<br><br> comb :: Maybe a -> (a -> Maybe b) -> Maybe b<br> comb = (>>=)<br><br>Which also of course means that you can typically use (>>=) instead of comb. 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> -- id is predefined in Haskell, definition given as example<br> id :: a -> a<br> id x = x<br><br> intID :: Int -> Int<br> 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, (>>=) 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. 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> (Just x) >>= k = k x<br> Nothing >>= _ = Nothing<br><br>So defining "comb = (>>=)" 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>