2

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?

6
  • Great question!, maybe also post in the regular stackoverflow?
    – zhekson
    Jun 14 at 2:45
  • You mean the haskell one?
    – KryptoKing
    Jun 14 at 3:26
  • Don't think there is one, but stackoverflow may help...
    – zhekson
    Jun 14 at 3:35
  • okay thanks ill do that
    – KryptoKing
    Jun 14 at 3:42
  • If you have some python experience, this might be helpful: github.com/cffls/py2hs#monad. It implements a monad in Python, explains the motivation, and then shows how to do the same thing in Haskell.
    – Jerry
    Jun 15 at 2:37

2 Answers 2

2

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.

My Explanation:

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.

TLDR:

basically, a monad allows the output of one function to be compatible with another.

0

I have done a couple of videos trying to explain monads, one explaining the formal implementation and one implementing a new monad from scratch

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"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.