[FoRK] Grappling with Monads
Damien Morton <
fork at bitfurnace.com
> on >
Fri Mar 31 00:41:10 PST 2006
Im learning Haskell, and 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. The problem with
this approach is 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, but the idea of Monads also has broader scope (which I am at
the foothills of exploring).
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
Now, lets say we had a sequence of functions, f1, f2, f3 that we wanted
to be called in that order. Assuming the functions are independent,
Haskell would normally evaluate them in whatever order it felt like, but
if we made each function dependant 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 produced and consumed by
the transformed functions.
That was my insight for today, rough and unpolished as it may be. Am I
on the right track? Anyone? Anyone? Bueler?
More information about the FoRK