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:
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?
defmacro
and it works great. And why do we need monads in Clojure if we have macros?
What is a Monad? A monad is an algebraic structure in category theory, and in Haskell it is used to describe computations as sequences of steps, and to handle side effects such as state and IO. Monads are abstract, and they have many useful concrete instances. Monads provide a way to structure a program.
Clojure has two built-in monads, "let" (identity monad) and "for" (sequence monad).
A monad is a design pattern that allows us to add a context to data values, and also allows us to easily compose existing functions so that they execute in a context aware manner.
A monad is more of a design pattern than a data structure. That is, there are many data structures that, if you look at them in the right way, turn out to be monads. The name “monad” comes from the mathematical field of category theory, which studies abstractions of mathematical structures.
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:
Monads and macros have nothing in common. They are used for solving different problems. In Clojure, the monad library uses macros quite extensively for implementing the syntactical "user interface" to monads. You may well use monads to implement some library's functionally and than add a layer of macros for the external interface.
As for "when would one use monads in Clojure", I see two use cases:
1) To implement stuff that makes sense in more than one monad, in order to do the job only once and "plug in" the monad later. Here is a nice illustration of this approach, though unfortunately (from a pedagogical point of view) for a rather non-trivial application: logic programming.
2) To implement a composition technique that can be formulated as a monad, in order to profit from the existing monad infrastructure.
Clojure has two built-in monads, "let" (identity monad) and "for" (sequence monad). Whenever you wish you could plug one of them into your code later on, you should use "domonad" instead. And whenever you wish you had something similar but not quite the same, you should consider writing your own monad.
This remains rather abstract, unfortunately. There aren't many published and polished code examples that use monads in Clojure at this moment. As more Clojurians get familiar with monads and more monad experts (usually coming from Haskell) use Clojure, this is likely to change. I have seen (but don't have at hand) monadic parsing done in Clojure, for example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With