I've been reading an excellent introduction to monads for Clojure programmers. The article illustrates that the Identity monad is functionally equivalent to Clojure's let and that the Sequence/List monad is equivalent to for.
When the article gets to monad transformers, it shows an example combining the Maybe and Sequence monads. Ok, so one reason for using a Sequence monad instead of a for is that I can transform it. However, transforming an Identity monad doesn't make sense to me - wouldn't that always be equivalent to just building up whatever the transforming monad is? For example, if I transformed Maybe with Identity - doesn't that just give me a Maybe, which would have been easier to declare directly?
Can someone clear up whether there's a practical use in Clojure for choosing an Identity monad over a let (perhaps I'm not thinking all the way through the implications of transformers?), or is it just there for theoretical completeness?
One good reason is that you can write monadic functions which are not tied to a particular monad and then execute them in a with-monad
block. identity-m
gives you the option of not involving any special monadic voodoo if you write (with-monad identity-m ...)
.
(Clearly, this won't work if your monadic function makes essential use of some properties of the monad its working with, like the availability of a getter and a setter for state etc. Not all monadic functions are like this, however.)