summaryrefslogtreecommitdiff
path: root/applicative
diff options
context:
space:
mode:
Diffstat (limited to 'applicative')
-rw-r--r--applicative/h1.hs14
-rw-r--r--applicative/h2.hs10
2 files changed, 24 insertions, 0 deletions
diff --git a/applicative/h1.hs b/applicative/h1.hs
new file mode 100644
index 0000000..b13c33d
--- /dev/null
+++ b/applicative/h1.hs
@@ -0,0 +1,14 @@
+import Control.Applicative
+
+data MyMaybe a = MyNothing | MyJust a
+ deriving (Show)
+
+instance Functor MyMaybe where
+ fmap _ MyNothing = MyNothing
+ fmap f (MyJust x) = MyJust (f x)
+
+instance Applicative MyMaybe where
+ pure x = MyJust x
+ MyNothing <*> _ = MyNothing
+ _ <*> MyNothing = MyNothing
+ (MyJust f) <*> (MyJust g) = MyJust (f g)
diff --git a/applicative/h2.hs b/applicative/h2.hs
new file mode 100644
index 0000000..6d9b801
--- /dev/null
+++ b/applicative/h2.hs
@@ -0,0 +1,10 @@
+import Control.Applicative
+
+newtype MyZipList a = MyZipList { getMyZipList :: [a] }
+
+instance Functor MyZipList where
+ fmap f (MyZipList x) = MyZipList (fmap f x)
+
+instance Applicative MyZipList where
+ pure x = MyZipList [x]
+ (MyZipList gs) <*> (MyZipList xs) = MyZipList (zipWith ($) gs xs)