Grappling with Monads
In learning Haskell, I have got to the point where I am trying to understand Monads.
Haskell is a lazy functional language, which means that almost everything in Haskell is expressed as a function, and that those functions are evaluated on an as-needed basis only. This approach means that you never quite know when or in what order your functions are going to be evaluated. It only becomes a problem when your functions have side-effects, such as interacting with the world outside Haskell. Input and output are the cannonical examples of such side effects.
Monads are Haskell’s way of controlling the order of evaluation. At least, thats the way they look to me right now.
So how do they work? Well, I only have a vague idea at the moment, but my current understanding is that they leverage Continuation Passing Style. Heres an example of CPS:
cps_add x y next = next x+y
-- here we pass in a function to be called with the result of our computation
Now, lets say we had a sequence of functions, f1, f2, f3, and we wanted them to be evaluated in that order. Normally, Haskell would evaluate them in whatever order it felt like, but if we made each function depend on the result of the previous one, then they would have to execute in sequence.
Monads seem to wrap up two facets of this requirement; the transformation of a sequence of functions into a CPS-like form such that each function depends on the result of the previous one, as well as defining the opaque data/class thingy that is passed between the transformed functions.
That was my insight for today, rough and unpolished as it may be. Am I on the right track? Anyone? Anyone? Bergman?

