From 52fcc103e1ae61ae3bb2e686763c246e2a266004 Mon Sep 17 00:00:00 2001 From: Mahdi Dibaiee Date: Wed, 11 Oct 2017 20:53:34 +0330 Subject: [PATCH] post(typoclassopedia): Monad#intuition --- ...-09-27-typoclassopedia-exercise-answers.md | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/_posts/2017-09-27-typoclassopedia-exercise-answers.md b/_posts/2017-09-27-typoclassopedia-exercise-answers.md index f0d160d..35d7854 100644 --- a/_posts/2017-09-27-typoclassopedia-exercise-answers.md +++ b/_posts/2017-09-27-typoclassopedia-exercise-answers.md @@ -630,3 +630,24 @@ instance Monad Maybe where (Var x) >>= f = Var (f x) (Node x) >>= f = Node (fmap f x) ``` + +## Intuition + +### Exercises + +1. Implement `(>>=)` in terms of `fmap` (or `liftM`) and `join`. + + **Solution**: + + ```haskell + a >>= f = join (fmap f a) + ``` + +2. Now implement `join` and `fmap` (`liftM`) in terms of `(>>=)` and `return`. + + **Solution**: + + ```haskell + fmap f a = a >>= (return . f) + join m = m >>= id + ```