]> git.rkrishnan.org Git - yorgey.git/blobdiff - misc/applicatives.hs
more notes from week11 lecture notes
[yorgey.git] / misc / applicatives.hs
index abf92313c5caa63aeec3b132eadd3306496e2a77..bdada3b44750ec82faa7c44da4f04665bdec9f0b 100644 (file)
@@ -1,3 +1,5 @@
+import Control.Applicative
+
 liftA2 :: Applicative f => (a -> b -> c) -> f a -> f b -> f c
 liftA2 h fa fb = (h `fmap` fa) <*> fb
 
@@ -27,4 +29,25 @@ h <$> fa = hfa
 --     f  has the type (a -> b), so pure f has the type f (a -> b)
 --   pure f <*> x => f (a -> b> <*> f a => f b
 
--}
\ No newline at end of file
+-}
+
+type Name = String
+
+data Employee = Employee { name :: Name
+                         , phone :: String }
+                deriving Show
+
+newtype ZipList a = ZipList { getZipList :: [a] }
+    deriving (Eq, Show, Functor)
+
+instance Applicative ZipList where
+  pure = ZipList . repeat
+  ZipList fs <*> ZipList xs = ZipList (zipWith ($) fs xs)
+
+instance Applicative ((->) e) where
+  -- pure :: a -> ((->) e a)
+  --       = a -> (e -> a)
+    pure = const
+-- f <*> x
+-- (e -> (a -> b)) <*> (e -> a) -> (e -> b)
+    f <*> x = \e -> (f e) (x e)