On Oct 26, 2012, at 11:06 AM, Brian Craft wrote:

> I've read about four tutorials on monads so far, but it still escapes me.
> 
> In fact, I'm still not sure what problem it solves. 


Monads are hard to understand, and I too found I wasn't the target audience for 
the explanations I read. I finally had to write my own as a way to force me to 
understand. Advertisement: the explanation is in chapter 10 and the optional 
chapters 15 and 16 of my book. (URL in my signature.) People seem to like mine 
because I start with the implementation, not the abstract ideas. Also, I lie 
when necessary on the way to the complete explanation.

The problem monads solve is twofold:

1. Suppose you have a series of computational steps. The results of those steps 
have to be combined in some way. A monad lets you move the combination rules 
away from the steps, so that you don't have to look at them. You just look at 
the steps and keep the rules in the back of your mind. That reduces code 
clutter.

2. Often, the rules are more general purpose than the steps. For example, it's 
common to want to break out of a series of steps when an error happens. Rather 
than scattering `if`s between some steps, you can point the Error monad at 
them. (When is that better than just using try/catch? -- that's still an open 
question to me.)

That said, the *really* general-purpose monads tend to get written into the 
language as special forms. Clojure's `let` and `for` are both monads, but you 
don't need to know that to use them. 

One thing that's important to realize about monads is that they apply the 
*same* rule to every step. That seems to make them clunky when you're working 
on problems that aren't nicely structured. But for certain structured problems, 
especially ones that lend themselves to combinations of predefined rules that 
apply to every step, monads are just The Right Thing.

Even if you don't use them, I'm inclined to think monads are a useful example 
of how to think about functions in a functional language. It helps you avoid 
just writing C code in Clojure. 

-----
Brian Marick, Artisanal Labrador
Contract programming in Ruby and Clojure
Occasional consulting on Agile
Writing /Functional Programming for the Object-Oriented Programmer/: 
https://leanpub.com/fp-oo


-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to