What is a Monad? An Explanation That Makes Sense
lambda-the-ultimate.org
lambda-the-ultimate.org
Further, as somebody who didn't previously know what a Monad is (and now does after reading the Burrito explanation linked in this thread), I'd suggest the Burrito explanation as an example of one that does in fact make sense (and is actually an explanation.)
Monads are essentially a design pattern, an abstraction, for dealing with items of some arbitrary type (call it "a") in some way.
Instead of working with values of type "a" directly, you work at one step removed. You hand the value of type "a" off to the monad, and it hands you back an opaque box (of type "M a") which logically contains your value. Now, instead of operating on the value directly, you instead hand the box a function that's supposed to apply to the contents of the box, and in return you're given back a new box with the function applied to the contents of the old box. That's basically it.
The interesting bit is that the monad - the box - sits between the function you're trying to apply, and the value it's supposed to apply to. That gives it the opportunity to do clever things. For example, the Maybe monad can keep track of whether the box is empty or not, and only apply the function you pass in if there's actually a value there. The List monad lets you apply operations to multiple values as if it were only one value (i.e. it's like an implied map). The IO monad takes in your function that's supposed to be applied to (e.g.) whatever was read from input, and hands you back a box that appears to contain the results of that operation. The catch is that the box is opaque - you can never look inside it - so there's no way for you to tell, from the outside, whether or not there were any side-effects. The IO monad can be looked at in another way: you can think of it as queuing up all the actions to perform on I/O, but actually building up an imperative program inside the box, which only ever gets opened up and evaluated after your program has finished its whole pure computation. (This is just a mental model for IO in the context of pure functional programming, with no side-effects.)
Putting a value into the box is the unit operator, also called return in Haskell. Passing in a function to the box and getting a new box in return is the bind operator, spelled >>= in Haskell, but simplified by do notation.
however, as the burrito explanation makes clear (and the apple thread confuses spectacularly) there's also some extra bits related to whether related boxes are identical or not. and it's kind-of obvious that these are important, because if you can't look into the box directly then you need to have some idea of what makes some boxes different while others remain equal.
And without some sufficiently abstract description of the thing, it's easy to fail to generalize from the seemingly unrelated concepts as a Maybe type and I/O.
If anyone else is in the same boat, I'd recommend this tutorial: http://www.haskell.org/all_about_monads/html/index.html
Particularly Part II, which shows how some of the types that you're already familiar with are defined as monads. (I'd recommend skimming Part II before reading Part I.)
For a typical developer with some experience in OO programming, a more useful analogy is with the command pattern.
In the command pattern, a "command" is an object which wraps a method call or some other statement or operation. The "invoker" is a client which can execute the command object. Command objects can be composed, so a compound command may consist of a sequence (or tree) of other command objects.
Hence a compound command is basically a small program, just with a lot of overhead since each statement is wrapped in a separate object. The benefit of the pattern is that the invoker can add some additional logic to the execution. For example the invoker could log state between each command execution, it could allow a client to single step through the command-chain, backtrack, replay and so on.
The command pattern could be used to e.g. implement exceptions in a language which didn't support exceptions natively. You just wrap all statements in command objects, and then have the invoker check the return value after each command execution - if the return value indicates an error, the invoker does not continue with the next command, but backtracks to the last "try" command.
In the same way it would also be possible to simulate continuations, parallel executions or other advanced language features using the appropriate invoker-logic. The pattern could be used to create all kinds of sublanguages with different semantics than the host language.
Monads is the equivalent of this in a functional language. Rather than wrapping operations in objects they are wrapped in closures.
The talk about apples trapped in boxes, burritos and so on, is just an attempt to explain the rather obvious fact that command objects are not interchangeable with ordinary values and statements. For example a command might calculate and return a number. But you can't add that command object to a ordinary number. You have to execute the command first and get the resulting number before it can be added to an ordinary number. Conversely you cannot insert an ordinary number or expression onto a chain of command objects. You have to wrap it in a command object first, before it can be composed with other commands.