Monads for Clojure Programmers
I think Monad make more sense when you understand the context and its additional constraints as to why you&#x27;d want to use Monads.<p>Imagine that you want to write code that is strictly pure, so that when evaluated, it doesn&#x27;t do any side effect, it only returns a pure description of the computation it did.<p>Here&#x27;s a simple example:<p><pre><code>    func announce-good-catch(fish)
      if(fish.size &gt; 5)
        print &quot;We found a big one!
      else
        print &quot;That one won&#x27;t even feed a cat, try again!&quot;
</code></pre>
How can you make this function pure? It technically prints something, yet you want to run it and not have it print, but instead return you something that describes what branch it took and what effect it would have done had it been impure.<p>You can do this:<p><pre><code>    func announce-good-catch(fish)
      if(fish.size &gt; 5)
        return [:print &quot;We found a big one!]
      else
        return [:print &quot;That one won&#x27;t even feed a cat, try again!&quot;]
</code></pre>
Now the function is pure, it doesn&#x27;t print anything, it evaluates the branch to take based on input and return to you a description of what it should do.<p>The problem is programming that way is hard, let&#x27;s add another function:<p><pre><code>    func get-fish(fish-id)
      return new Fish(db.query(&quot;select * from fish where id = &quot; + fish-id)
</code></pre>
Again this isn&#x27;t pure, so let&#x27;s make it pure:<p><pre><code>    func get-fish(fish-id)
      return [:new Fish [:query [:get :db] [&quot;select * from fish where id = &quot; + fish-id]]])
</code></pre>
Already this is getting pretty complicated, getting the DB is impure so return a description of it, querying the DB is impure, so return a description of it, Fish can&#x27;t be created if we don&#x27;t execute the side-effect, so take note that you want to create a Fish after those two side-effects were to actually happen.<p>Now combine get-fish and announce-good-catch(fish)?<p><pre><code>    announce-good-catch((get-fish 10))
</code></pre>
Well this works in the impure version, but this doesn&#x27;t work for our pure versions because now the input to announce-good-catch isn&#x27;t an actual Fish but instead it&#x27;s this:<p><pre><code>    [:new Fish
     [:query
      [:get :db]
      [&quot;select * from fish where id = 10&quot;]]]
</code></pre>
And so it&#x27;s not going to work, you can&#x27;t compose those pure functions as you&#x27;d normally would.<p>Here is where Monads become really handy, they put all this behind an abstraction that lets you with added syntax sugar feel like your code is written in the impure style, yet under the hood it will convert things to a description of all side-effects and whatever pure behavior is pending the result of those. And it&#x27;ll let you compose those descriptions together so that from the programmer point of view the code you write looks and feels almost the same as the impure version.<p>This is the problem Monad solves.<p>Now once you have that description, eventually you want it to actually run for real so that side-effects and all other described pending behavior gets executed. But what people realized here is you can kind of abuse this a bit, where you could choose to interpret the description in new ways, or manipulate the description prior to executing it. Basically whatever executes the description for real is in charge of how to do that and it could choose to change some of the assumptions like the evaluation order or the thread on which thing execute, etc.<p>Thus once you have Monads, you can also use them to manipulate evaluation order and semantics.<p>Now if you don&#x27;t care about constraining yourself to pure functions, all this can seem like overkill, and it kind of is.<p>Obviously, people will tell you it&#x27;s a good idea to make everything pure, because you can more easily test things.<p>And people will tell you, even if you don&#x27;t care about the purity, how Monads let you manipulate evaluation order and semantics of the code can be cool and allow for some neat features, so maybe that&#x27;s something you&#x27;d find useful, such as adding tracing throughout automatically, or automatically parallelizing things that don&#x27;t depend on each other, etc.<p>This is where some people say that Macros can often be used to similar effect. Because macros, a bit like Monads, operate on the description, macros on the code&#x27;s AST itself, Monads on the captured Monadic composition. So a macro can also rewrite things to change their evaluation order or semantics.<p>I wouldn&#x27;t call myself an expert on all this yet, more of an enthusiast, but that&#x27;s how I would contextualize it.