1 import Control.Applicative
3 liftA2 :: Applicative f => (a -> b -> c) -> f a -> f b -> f c
4 liftA2 h fa fb = (h `fmap` fa) <*> fb
6 -- In fact, this pattern is so common that Control.Applicative defines (<$>) as a synonym for fmap,
8 (<$>) :: Functor f => (a -> b) -> f a -> f b
11 liftA2 h fa fb = h <$> fa <*> fb
17 -- (a -> b -> c) -> f a -> f (b -> c)
20 -- (<*>) :: f (a -> b) -> f a -> f b
22 -- f (b -> c) -> f b -> f c
25 -- f `fmap` x === pure f <*> x
27 -- lhs = (a -> b) `fmap` f a gives a value of type f b
29 -- f has the type (a -> b), so pure f has the type f (a -> b)
30 -- pure f <*> x => f (a -> b> <*> f a => f b
36 data Employee = Employee { name :: Name
40 newtype ZipList a = ZipList { getZipList :: [a] }
41 deriving (Eq, Show, Functor)
43 instance Applicative ZipList where
44 pure = ZipList . repeat
45 ZipList fs <*> ZipList xs = ZipList (zipWith ($) fs xs)
47 instance Applicative ((->) e) where
48 -- pure :: a -> ((->) e a)
52 -- (e -> (a -> b)) <*> (e -> a) -> (e -> b)
53 f <*> x = \e -> (f e) (x e)