You Could Have Invented Monads! (And Maybe You Already Have.)

`f :: a -> b`

f' :: a -> m a

unit :: a -> m a

`f' * g' = (bind f') . (bind g')`

```
```bind f xs = concat (map f xs)

`bind unit xs = concat (map unit xs)`

`unit x = [x] `

```
```

`bind unit xs `

= concat (map unit xs)

= concat (map unit [x1, x2, ...])

= concat [unit x1, unit x2, ...]

= concat [[x1], [x2], ...]

= [x1, x2, ...]

= xs

`f' = lift f`

```
```

`lift f = unit . f`

`unit`

(or `return`

) can directly act on an ordinary value only, but not on a monadic value. To act on a monadic value, you need to `bind`

it.

How come we do not need to lift `return`

?

`f :: a -> b `

```
```liftM :: Monad m => (a -> b) -> m a -> m b

return :: a -> m a

(liftM f) :: m a -> m b

`(>>=) :: Monad m => m a -> (a -> m b) -> m b`

`lifeM`

cannot be applied to `return`

at all.

`unit`

(or `return`

) is neither a pure function nor a monadic function. Instead, it is an half-monadic function, meaning that while its input is an ordinary value, its output is a monadic value.

`(bind return xs) -> ys`

`(bind return)`

applies to `xs`

.

`return`

applies to `x`

.

`liftM`

is merely `fmap`

implemented with `(>>=)`

and `return`

…

— Wikibooks on *Haskell/Understanding monads** *

— Me@2016-01-26 03:05:50 PM

2016.01.30 Saturday (c) All rights reserved by ACHK