<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;">Got it! I figured there must be some way to unpack it.<br><br>My goodness, there are so many functions I'm not even aware of. Has anyone ever counted them all?<br><br>Thanks.<br><br>Michael<br><br>--- On <b>Tue, 4/21/09, Tony Morris <i><tonymorris@gmail.com></i></b> wrote:<br><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px;"><br>From: Tony Morris <tonymorris@gmail.com><br>Subject: Re: [Haskell-cafe] Getting the x out<br>To: "michael rice" <nowgate@yahoo.com><br>Cc: haskell-cafe@haskell.org<br>Date: Tuesday, April 21, 2009, 8:54 PM<br><br><div class="plainMail">You mean, the x out of *Maybe* x even. In the very literal sense, the<br>assumption that there is an x in "Maybe x" is false -- there may not be<br>one since it is maybe, but not necessarily, x. IT's a bit like the use<br>of null that you
might have seen in other languages where you might have<br>a value or you might have null. What you can do however, is say "give me<br>the x if there is one, otherwise, use this value".<br><br>This is the fromMaybe function.<br><br>Prelude Data.Maybe> let safeDivision x y = if y == 0 then Nothing else<br>Just (x/y)<br>Prelude Data.Maybe> 3 + (42 `fromMaybe` safeDivision 10 5)<br>5.0<br>Prelude Data.Maybe> 3 + (42 `fromMaybe` safeDivision 10 0)<br>45.0<br><br><br><br>michael rice wrote:<br>> How do I get the x out of Just x?<br>><br>> Michael<br>><br>> =============<br>><br>> safeDivision :: Float -> Float -> Maybe Float<br>> safeDivision x y = if y == 0 then Nothing else Just (x/y)<br>><br>> *Main Data.List> safeDivision 10 5<br>> Just 2.0<br>> *Main Data.List> 3 + (safeDivision 10 5)<br>><br>> <interactive>:1:0:<br>> No instance for (Num (Maybe
Float))<br>> arising from a use of `+' at <interactive>:1:0-22<br>> Possible fix: add an instance declaration for (Num (Maybe Float))<br>> In the expression: 3 + (safeDivision 10 5)<br>> In the definition of `it': it = 3 + (safeDivision 10 5)<br>> *Main Data.List><br>><br>><br>> ------------------------------------------------------------------------<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>> <br><br>-- <br>Tony Morris<br><a href="http://tmorris.net/"
target="_blank">http://tmorris.net/</a><br><br><br></div></blockquote></td></tr></table><br>