[Haskell-beginners] Maybe a -> Maybe b
Olivier Iffrig
olivier at iffrig.eu
Fri Aug 25 12:27:57 UTC 2017
PICCA Frederic-Emmanuel wrote (2017-08-25 08:05:34):
> Hello, I have this
>
> data Proxy = Proxy String
>
> And I want to write a function
>
> f :: Maybe Proxy -> Maybe String
> f ma = case ma of
> (Just (Proxy a)) -> Just a
> Nothing -> Nothing
>
> I was wondering if there is no simpler way to do this ?
Maybe implements the Functor type class (as an exercise, you can try to
figure out the implementation), therefore you can simply use fmap, which
in the case of Maybe, has type (a -> b) -> Maybe a -> Maybe b
You just need a function to "unwrap" the String inside of the Proxy, and
you're done.
--
Olivier
More information about the Beginners
mailing list