3 ms·
Monads can encode state but they don't have to. I tend to think of a monad in computer science as something which is constructed from a type and a computation
by seanhunter 2y ago
Monads can encode state but they don't have to.
I tend to think of a monad in computer science as something which is constructed from a type and a computation and wraps the return values of that computation in that type. That allows you to bake in "context" into that type which could be state in the conventional sense but it could be something else.
To make this practical, let's make a simple example of a simple somewhat useful monad with no state. Imagine you have all the regular trigonometric functions sine, cosine etc which accept radians[1] and you need versions which work in degrees. You could make a unit conversion monad which converts arguments on the way in and wraps the result in an inverse conversion monad such that if you passed it to arcsine arccosine etc on it it would know and return degrees on the way out.
Neither of those monads would have any state in the usual computer science sense, the main conversion is just multiplying all the inputs by pi/180 before calling the wrapped function and constructing a result monad type so the inverse trig functions do the opposite of that.
[1] ie the correct versions. Fight me physicists and engineers and you freaks who use gradians whoever you are[2].
[2] I think it's surveyors but I could be wrong.