Easier Macros with Syntax Quoting

Let’s return to our arithmetic-if macro:

 (​defmacro​ arithmetic-if [n pos zero neg]
  (list ​'cond​ (list ​'pos?​ n) pos
  (list ​'zero?​ n) zero
  :else neg))

We’re working pretty hard to produce that cond expression. After all, it’s just some boilerplate code with a few custom values slotted in here and there. If this sounds like a familiar problem, it should. People who develop web applications frequently need to mix context-specific data—perhaps today’s date or the current share price of Anacott Steel—into a skeleton of otherwise static HTML.

The solution in both situations is the same: use a template. You cook up the static bulk of your output as a sort of skeleton, which includes special markers that ...

Get Getting Clojure now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.