[Haskell-cafe] Getting the x out
Mattias Bengtsson
moonlite at dtek.chalmers.se
Tue Apr 21 21:02:16 EDT 2009
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
More information about the Haskell-Cafe
mailing list