3 ms·
I can take a stab at this, but someone else might be more comprehensive. 1) Grade-school language is too hard. I am not a mathematician, though, nor a hardcore
by wonderzombie 13y ago
I can take a stab at this, but someone else might be more comprehensive.
1) Grade-school language is too hard. I am not a mathematician, though, nor a hardcore Haskeller. But maybe that's good enough. Let's see.
A Monad is typically a parametric datatype which stores a value with some context. (A "parametric datatype" is just a concise way of describing something like scoped_ptr<Foo>.)
An example would be Maybe. A Maybe is a parametric type, much like your scoped_ptr<>. An example would be Maybe Int.
A Maybe Int can be one of two things: Nothing, or Just x, where x is any Integer. Examples: Just 2, Just -8, Just 0.
Monads support two functions: bind and return. Maybe is no exception to this.
Return (unfortunate name) puts a value inside a monad, in a minimal context. So if you apply "return" to the number 6, you get Just 6.
Notice that unless you implement your own arithmetic routines, you can't (hand-wave) use operations like + or - on a Maybe Int. This is what the second operation, bind, is for.
Bind applies a function to the value inside the monad, with the stipulation that the function in question itself results in a monad. (Hold on to that thought.)
Bind is what you'd use to add (e.g.) 3 to Just 6, or how we'd divide Just 9 by 3.
Why would Nothing be useful? Well, in this dumb example, you might want to implement a division operator. So what if you have Just 0 and attempts to divide it by 3? Well, since Nothing is a valid value for Maybe Int, you could just return that instead of throwing an exception.
By convention many functions which can result in a failed computation return nothing. So a key lookup in a map, for instance, will return Maybe MyVal. It's incumbent on the caller to check for whether it's an instance of Nothing or Just MyVal; there's no "null" in the picture. (I'm hand-waving here; there's a ton of syntactic stuff in Haskell to make this very simple.)
I'm not going to talk about function composition, except to say that Bind lends itself well to that; you can bind multiple items expecting such as a Maybe Int in a row, since a stipulation of bind is the result has to be a monad.
2) They're probably doable in C++ with template magic, but I would never ask someone to subject themselves to that.
3) You won't necessarily use them but they are a useful concept to have in mind. Maybe itself crops up in such as Scala or other places, as an Option type.
It's conceivable that instead of returning null/None or the moral equivalent you design your classes such that nobody returns null. Instead you might return an empty Person class. Calls on it which do nothing could be a no-op instead of crashing the program, or what have you; you can define what behavior you want to occur rather than a crash/stack trace.
I probably didn't do a good job explaining this but I hope it helps. I tried to define monads in a more operational way rather than explaining the theory, which I myself can't explain in a systematic or comprehensive way.