There are too many tutorials out there on monads that say… “Look! here is a case where we can use a monad” or “This is what a monad is for”. What I want to know is what are some of the steps that people use to come to the conclusion that they can say to themselves – “Gee Whiz! It looks like we can use a monad here!”
So when someone tells me… “(blah) has nothing to do with a monad…”, it really doesn’t help me answer my questions, which are:
- How do I go about determining what sort of patterns in programs could be expressed using monads?
- How can I write my own customised monad when I have identified the potential to use them?
I started a really long question here about monads if anybody is interested to help – Map and Reduce Monad for Clojure… What about a Juxt Monad?.
Back to this Question:
When should we use a monad instead of a macro and vice-versa?
- I’ve read articles and watched presentations that say… ‘Monads are used for DSL abstraction’ …. but most of the Clojure DSL libraries (eg. hiccup and korma) are using
defmacroand it works great.
And why do we need monads in Clojure if we have macros?
I’ve been using Clojure for two years now and the only time I ever used monads was as an exercise to show that it could be done. I’ve never needed them for “real” code.
Monads are much more common in Haskell because:
My suggestion would be to focus on standard functional programming in Clojure. Unless you see that you really need monads then I wouldn’t invest too much time in trying to bring them in.
Macros are a slightly different issue: they are for compile-time code generation and extensions to the language syntax (which might include DSLs, although DSLs don’t necessarily need macros). I use macros when both of the following are true:
P.S. If you are genuinely interested in monads for Clojure, here are two videos I personally found quite good: