November 2017
Intermediate to advanced
670 pages
17h 35m
English
Maybe is a functor that maps every type to the same type with an additional Nothing value. Maybe is like an optional value (note that types are the objects in our category):
data Maybe a = Just a | Nothing
The value of Maybe Int can be either just a number, such as Just 2, or Nothing.
The Maybe type maps types to types. For example, it maps Char to Maybe Char. fmap, defined in the following snippet, shows how every a -> b function has a corresponding version, Maybe a -> Maybe b, which just returns Nothing when given Nothing and behaves normally otherwise:
instance Functor Maybe wherefmap f Nothing = Nothingfmap f (Just x) = Just (f x)