Re: [Haskell-cafe] Getting the x out

Got it! I figured there must be some way to unpack it.
My goodness, there are so many functions I'm not even aware of. Has anyone ever counted them all?
Thanks.
Michael
--- On Tue, 4/21/09, Tony Morris
How do I get the x out of Just x?
Michael
=============
safeDivision :: Float -> Float -> Maybe Float safeDivision x y = if y == 0 then Nothing else Just (x/y)
*Main Data.List> safeDivision 10 5 Just 2.0 *Main Data.List> 3 + (safeDivision 10 5)
<interactive>:1:0: No instance for (Num (Maybe Float)) arising from a use of `+' at <interactive>:1:0-22 Possible fix: add an instance declaration for (Num (Maybe Float)) In the expression: 3 + (safeDivision 10 5) In the definition of `it': it = 3 + (safeDivision 10 5) *Main Data.List>
------------------------------------------------------------------------
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Tony Morris http://tmorris.net/

Hi
It's not too hard. You wanted a function that converted Maybe a -> a,
you just Hoogle for it:
http://haskell.org/hoogle/?hoogle=Maybe+a+->+a
Thanks
Neil
On Wed, Apr 22, 2009 at 2:07 AM, michael rice
Got it! I figured there must be some way to unpack it.
My goodness, there are so many functions I'm not even aware of. Has anyone ever counted them all?
Thanks.
Michael
--- On Tue, 4/21/09, Tony Morris
wrote: From: Tony Morris
Subject: Re: [Haskell-cafe] Getting the x out To: "michael rice" Cc: haskell-cafe@haskell.org Date: Tuesday, April 21, 2009, 8:54 PM You mean, the x out of *Maybe* x even. In the very literal sense, the assumption that there is an x in "Maybe x" is false -- there may not be one since it is maybe, but not necessarily, x. IT's a bit like the use of null that you might have seen in other languages where you might have a value or you might have null. What you can do however, is say "give me the x if there is one, otherwise, use this value".
This is the fromMaybe function.
Prelude Data.Maybe> let safeDivision x y = if y == 0 then Nothing else Just (x/y) Prelude Data.Maybe> 3 + (42 `fromMaybe` safeDivision 10 5) 5.0 Prelude Data.Maybe> 3 + (42 `fromMaybe` safeDivision 10 0) 45.0
michael rice wrote:
How do I get the x out of Just x?
Michael
=============
safeDivision :: Float -> Float -> Maybe Float safeDivision x y = if y == 0 then Nothing else Just (x/y)
*Main Data.List> safeDivision 10 5 Just 2.0 *Main Data.List> 3 + (safeDivision 10 5)
<interactive>:1:0: No instance for (Num (Maybe Float)) arising from a use of `+' at <interactive>:1:0-22 Possible fix: add an instance declaration for (Num (Maybe Float)) In the expression: 3 + (safeDivision 10 5) In the definition of `it': it = 3 + (safeDivision 10 5) *Main Data.List>
------------------------------------------------------------------------
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Tony Morris http://tmorris.net/
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

michael rice wrote:
Got it! I figured there must be some way to unpack it.
If you peek at the thread about getting a value out of IO [1], you will see some similarities. If you look at my response [2], you will see that the functions I suggested for IO are exactly the same as the functions you may want to consider for this case! fmap, liftA, liftM, (<$>) :: Functor f => (a -> b) -> (f a -> f b) (<*>), ap :: Applicative f => f (a -> b) -> (f a -> f b) (=<<) :: Monad m => (a -> m b) -> (m a -> m b) So, for Maybe, you have: (<$>) :: (a -> b) -> (Maybe a -> Maybe b) (<*>) :: Maybe (a -> b) -> (Maybe a -> Maybe b) (=<<) :: (a -> Maybe b) -> (Maybe a -> Maybe b) You will find that a *lot* of the functions you learn in Haskell are actually very general, and once you internalize what they *really* do, you will suddenly have entire classes of problems solved by just a few functions. - Jake [1] http://www.haskell.org/pipermail/haskell-cafe/2009-April/059834.html [2] http://www.haskell.org/pipermail/haskell-cafe/2009-April/059852.html
participants (3)
-
Jake McArthur
-
michael rice
-
Neil Mitchell