|
|
|
|
|
by mlazos
41 days ago
|
|
What also makes the monad more interesting than just composition or flattening is that when executing the ordered computation, you can decide next computations based on previous results. This was what really made it click for me. |
|
The "ordered computation" isn't unique to monads, it applies to all of these when you chain them. For monads particularly though, you can't begin the continuation until the previous result has been generated - whereas with a comonad, you can begin the continuation right away and extract the value afterwards (inside the continuation).
Can also think of it like a producer/consumer or push/pull thing. Monads push values into some type `t`, and comonads pull values out of `t`.
The naming of `return` is perhaps unfortunate as it gives the impression you're returning a value (done to make do-notation look like imperative code) - but monads don't return values. `return` produces a computation.
`coreturn` is usually called `extract`, which is better naming - it extracts a value from a computation.
The order of arguments is also unfortunate because it makes the above pattern less obvious - they're usually written as:
So it's not obvious that `fmap` is transforming a function `(a -> b)` into one `(f a -> f b)`, etc.