Hacker News new | ask | show | jobs
by goto11 1468 days ago
This describes some monads but not others. IO and State can be thought of as environments supporting effects, but it doesn't really make sense to think of a List like that.

The problem with many monad explanations is they only explain particular monads but then other monads become even more confusing.

1 comments

I've heard the List monad described as modeling nondeterminism as an effect. As you chain flatMaps, at each stage, you're consuming a discrete set of possible inputs and producing a new set of potential outputs per input.

So it's a little different from thinking of your program as something that is once. Instead, you have your program being run multiple times, with the environment injecting different at each stage.

I think the Future/Promise monad is similar. The environment is taking responsibility for pausing and resuming computation.

But I'm curious if you have other monads in mind where the analogy fails.

A ordinary list expression like [x + 2 | x <- [7, 9, 13]] is not effectful or non-deterministic. It is completely deterministic and have no side effects.
Correct that there's no literal effect, but conceptually, if you wanted to model a particular sort of nondeterminism as a monad, you could use one indistinguishable from List.

And I think it holds just fine to think of the monad as injecting data into a sequential computation before each step and ingesting output after each step (boxed in the monadic container).

Which reminds me, that boxing is also part of the power of monads. It means you can nest steps of a monadic computation, instead of just chaining them, as a functor allows. This stimulates lexical scope -- bindings that exist for all statements after the one that defines them.