Hacker News new | ask | show | jobs
by sdegutis 3507 days ago
> the list monad is used to model non-determinism

See this is the kind of thing that made learning Haskell so hard for me. I'm not a math guy, I'm terrible at math. I have no idea what "non-determinism" means, and I'd venture to guess that list monad is used to model "ordered collections of things".

3 comments

Scala calls the monad bind "flatMap". Java actually implements it for Streams.

If you have a `Stream<Deck> decks;` of card decks, and want to get all the cards from all the decks, you might try to do something like `decks.map(deck -> deck.getCards().stream())`, but that will give you the type `Stream<Stream<Deck>>`. That sucks, we just want a single stream, not a stream of streams, so we call the function that flattens at the same time. `decks.flatMap(deck -> deck.getCards().stream())`.

Nondeterminism is just a standard use for this construction: given one choice, generate more choices, given that choice, generate more choices, do some computation with the choices, and then return the list of possible final results.

I'm not a math guy either and I did indeed find it difficult to penetrate the inner sanctum of Haskell for this very reason. I won't say I've completely succeeded in that effort, but I've come quite a long way.

I'm fairly certain that in this context, non-determinism means that there can be more than one answer/result.

"non-determinism" implies unordered.
See, to me, the sequence of keypresses I make while banging on my keyboard seem like it could be represented as an ordered list. I may be missing something, though.
If something is unordered it does not mean it may not be ordered. It only says you cannot assume that it is ordered. (A Collection in Java/C# is an unordered contract, but a List is also a Collection.)

But the idea of a list also suggests an ordering to me.