[Haskell-cafe] Re: A function for Maybes
afalloon at synopsys.com
Thu Jan 25 14:12:48 EST 2007
John Ky wrote:
> Is there a built-in function that already does this?
Usually, when I have a question like this, I try Hoogle first:
Unfortunatly, the right answer (fmap) is on the second page of results.
(I am really excited for the new version of Hoogle, its supposed to be
pretty close to release)
> foo :: (a -> b) -> Maybe a -> Maybe b
> foo f m
> | isNothing m = Nothing
> | otherwise = Just (f (fromJust m))
> *Main> foo (+2) (Just 3)
> Just 5
> *Main> foo (+2) Nothing
Prelude> fmap (+2) (Just 2)
Prelude> fmap (+2) Nothing
it works over all Functors, so list also works:
Prelude> fmap (+2) [2,3]
Prelude> fmap (+2) 
and Map and so on.
More information about the Haskell-Cafe