summaryrefslogtreecommitdiff
path: root/monad
diff options
context:
space:
mode:
Diffstat (limited to 'monad')
-rw-r--r--monad/h1.hs3
-rw-r--r--monad/h2.hs5
-rw-r--r--monad/h21.hs3
-rw-r--r--monad/h3.hs11
4 files changed, 22 insertions, 0 deletions
diff --git a/monad/h1.hs b/monad/h1.hs
new file mode 100644
index 0000000..a6cc093
--- /dev/null
+++ b/monad/h1.hs
@@ -0,0 +1,3 @@
+instance Monad [] where
+ return a = [a]
+ (>>=) as f = concatMap f as
diff --git a/monad/h2.hs b/monad/h2.hs
new file mode 100644
index 0000000..fce9d81
--- /dev/null
+++ b/monad/h2.hs
@@ -0,0 +1,5 @@
+instance Monad ((->) e) where
+ return f = \_ -> f
+ (>>=) f1 f2 = \r -> f2 (f1 r) r
+ -- f1 :: m a
+ -- f2 :: a -> m b
diff --git a/monad/h21.hs b/monad/h21.hs
new file mode 100644
index 0000000..c843dcb
--- /dev/null
+++ b/monad/h21.hs
@@ -0,0 +1,3 @@
+(>>=) m f = join $ fmap f m
+join x = x >>= id
+fmap f x = x >>= (return . f)
diff --git a/monad/h3.hs b/monad/h3.hs
new file mode 100644
index 0000000..9439331
--- /dev/null
+++ b/monad/h3.hs
@@ -0,0 +1,11 @@
+data Free f a = Var a
+ | Node (f (Free f a))
+
+instance Functor f => Functor (Free f) where
+ fmap g (Var a) = g a
+ fmap g (Node a) = fmap g a
+
+instance Functor f => Monad (Free f) where
+ return a = Var a
+ (Var a) >>= g = g a
+ (Node a) >>= g = fmap g a