Typeclassopedia presents the following exercise:
Maybe
can easily be made an instance ofApplicative
; writing such an instance is left as an exercise for the reader.
Here's my implementation:
data Option a = Some a
| None deriving Show
instance Functor Option where
fmap _ None = None
fmap f (Some x) = Some (f x)
instance Applicative Option where
pure x = Some x
(Some f) <*> (Some x) = Some (f x)
_ <*> _ = None
How does this look? In particular, I'm curious if the second line of the Applicative Option
looks good.
Should I be using fmap
on the right-hand side instead of Some (f x)
?