
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>

If you want to just get the value out, meaning you'll get a program error if it happens to be Nothing, then you can use Data.Maybe.fromJust. But usually, you'd want to preserve the Nothing. Applicative or Monad is pretty good for this: import Control.Applicative (3+) <$> safeDivision 10 5 the result will be Just 5.0 in this case, but if the division was incorrect it would be nothing. If you want to do something else, you can either pattern match on it: case safeDivision 10 5 of Just x -> -- do something with x Nothing -> -- do something else or use some functions from Data.Maybe. Say you want to evaluate to 1 instead of Nothing: import Data.Maybe fromMaybe 1 (safeDivision 10 5) -Ross On Apr 21, 2009, at 8:49 PM, 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

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/

On Tue, 2009-04-21 at 17:49 -0700, 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)
This would do: case safeDivision 10 5 of Just x -> 3 + x Nothing -> Nothing The Maybe-monad might make this a bit more convenient: ghci> let plusthree a = do a' <- a; return (a'+3) ghci> plusthree (Just 5) Just 8

On Wed, Apr 22, 2009 at 2:49 AM, michael rice
How do I get the x out of Just x?
Hi Michael, in your code you're using Maybe to inform the caller of safeDivision about an exceptional situation. This way, you made a full coverage of all the input cases and nothing is left out, i.e. you created a total function (which is GOOD). If you introduced the Nothing case, you just don't want to ignore it. Also, the type system is forcing you to take the Nothing case into account so you can handle it properly. Hence, you might try something like the "maybe" function, which accounts for the Nothing case. If you'd use fromJust, the Nothing case would remain uncovered, leading you to an unhandled exception, which conflicts with your safeDivision definition. Cristiano
participants (5)
-
Cristiano Paris
-
Mattias Bengtsson
-
michael rice
-
Ross Mellgren
-
Tony Morris