
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: http://www.haskell.org/hoogle/?q=%28a+-%3E+b%29+-%3E+Maybe+a+-%3E+Maybe+b 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 Nothing
Prelude> fmap (+2) (Just 2) Just 4 Prelude> fmap (+2) Nothing Nothing it works over all Functors, so list also works: Prelude> fmap (+2) [2,3] [4,5] Prelude> fmap (+2) [] [] and Map and so on. -- Alan Falloon