Ask HN: Does anyone know what a monad is?
I’ve started learning haskell a few weeks ago and I’ve been recently trying to understand what a monad is. I can’t seem to find and explanation that makes sense. Every other comment on the subject seems to be about how no one can explain what is a monad.
For example I don’t understand (beyond the concept as a whole): 1. How do monads allow side effects if you can’t have side effects in Haskell? 2. Are monads just a name for a reccuring way to aproach a problem? 3. In what scenario would you use a monad, or for what type of problem?
7 comments
[ 2.7 ms ] story [ 32.9 ms ] threadhttps://en.wikipedia.org/wiki/Clean_(programming_language)
A monad is kind of like a generic class to boxes that adds additional logic to the data it boxes without actually caring about the data itself.
My goto monad for that concept is a linked list and the map operator. So an instance of the linked list might be Node(5) -> Node(7) -> EmptyList. Now let's call map with a function f(x) = str(x) + " * 2 = " + str(2x). This gives us Node("5 2 = 10") -> Node("7 * 2 = 14") -> EmptyList.
Now let's separate the monad from this. The monad is the structure and logic around the data and the function that we provide. The monad doesn't care what data it holds and is doesn't care what function we provide. It only defines the structure and how functions are applied to the data it holds.
"Functor", "Applicative", and "Monad" are all just generalizations of the concept of `map` and `flatMap`.
There's nothing really more complex to it, besides how you squint at various things (like functions) to fit them into the concept of `map` and `flatMap`.To answer your questions more directly: