4-Applicative-3-Instances
- Implement an instance of
Applicative
forMaybe
.
instance Applicative Maybe where
pure g = Just g
(<*>) (Just g) = fmap g
(<*>) Nothing = \_ -> Nothing
Identity law:
pure id <*> v = (Just id) <*> v = fmap id v
fmap id Nothing = Nothing
fmap id (Just x) = Just (id x) = Just x
Homomorphism:
Interchange:
-- show:
u <*> pure y = pure ($ y) <*> u
-- u = Nothing
Nothing <*> pure y = Nothing = fmap ($ y) Nothing
= pure ($ y) <*> Nothing
-- u = Just g
Just g <*> pure y = Just (g y) = Just (($ y) g)
= fmap ($ y) (Just g) = pure ($ y) <*> Just g
Composition:
- Determine the correct definition of
pure
for theZipList
instance ofApplicative
—there is only one implementation that satisfies the law relatingpure
and(<*>)
.