[Haskell-beginners] Thinking about monads

Michael Mossey mpm at alumni.caltech.edu
Sat Apr 11 22:05:34 EDT 2009


I'm getting a better grasp on monads, I think. My original problem, I think, was that 
I was still thinking imperatively. So when I saw this:

class Monad m where

    (>>=) :: m a -> (a -> m b) -> m b

I didn't understand the "big deal". I thought, okay so you "do" something with the 
function (a -> m b) and you "arrive" at m b. Now I realize that chaining a sequence 
of Monads via that function, a -> m b, means that a is available to any function 
further down the line, because it is an argument to a series of nested functions. So,


doSomething =
   thing >>= \x ->
   thing2 >>= \y ->
   return (x,y)

 >>= produces a series of nested functions in which all the arguments of earlier 
functions are available to later functions: x and y are available to "return" because 
they are arguments of functions further up in the chain. This resembles imperative 
code in which any variable, once set, is available further down.

Any clarifications welcome.

-Mike


More information about the Beginners mailing list