Browse Clojure Foundations for Java Developers

Writing Basic Macros

How to generate code with syntax-quote, unquote, and gensyms—without surprises.

The safest way to write a macro is to start with a function, then convert it to a macro only when you have a concrete reason.

When you do write a macro, you usually rely on:

  • Syntax quote ` to build code templates.
  • Unquote ~ and unquote-splicing ~@ to insert values/forms into those templates.
  • Gensyms (auto-generated symbols) to avoid variable capture.

Your goal is to produce boring, predictable expansions that read like code you’d happily review.

In this section

Revised on Friday, April 24, 2026