I was asked by a friend trying to learn plutus what a monad was, so I began talking about category theory. It seems that this confused him further how would you explain monads to a noob?
Whats your mental model for dealing with them?
Cardano Stack Exchange is a question and answer site for users and developers of the Cardano cryptocurrency ecosystem. It only takes a minute to sign up.Sign up to join this community
Just read one definition:
A monad is just the minimal amount of structure needed to overload a function composition in a way that performs an extra computation on the intermediate value.
In a single program, you will licky have many functions such as those dealing with IO and others dealing with Computation. Function composition then enables us to chain functions together (basically piping the output of one function into another).
This works well when the later functions expect the type of data given to them, but will fail if the data type is not what's expected. To get around this we normally refactor functions using factories or decorates in languages such as python.
Now in functional programming, we have an alternative way of doing this and that is the monad. It basically can be thought of as a tuple of operations attached to a data type (or container of data). This tuple of operations is a wrapping and an unwrapping function that allow polymorphic (arbitrary) data types to work with arbitrary functions.
basically, a monad allows the output of one function to be compatible with another.
the second in particular shows you how monads do allow side effects by considering the container (your
Monad instance) as a context
this way the side effect is not "outside" the function, since the monad is part of the function
this explanation is very raw but in the second video you can see how the bind operator (
>>=) handles all the "side effects"