post(typoclassopedia): Monad#intuition

This commit is contained in:
Mahdi Dibaiee 2017-10-11 20:53:34 +03:30
parent cd5b18a247
commit 52fcc103e1

View File

@ -630,3 +630,24 @@ instance Monad Maybe where
(Var x) >>= f = Var (f x) (Var x) >>= f = Var (f x)
(Node x) >>= f = Node (fmap 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
```