liveBook · Manning

Quote form

As explained in Clojure: The Essential Reference

quote is a Lisp special form that prevents an expression from being evaluated and returns it as data.

The quote form prevents evaluation and returns its single input expression as data. It is a special form used mainly in metaprogramming, especially when macros need to receive or construct Clojure forms. section 4.3 section 4.0

Use quote when a form must remain literal:

  • Write (quote expr), or use the equivalent reader-macro shorthand 'expr. section 4.3
  • Pass exactly one required expression; the result is that unevaluated expression. section 4.3
  • Use quoted symbols as data for runtime introspection functions such as resolve. section 4.3
  • Supply quoted forms to macroexpand when inspecting macros without executing them. section 4.2

Returning code as data

(quote (+ 1 2)) ;; (+ 1 2) '(+ 1 2) ;; (+ 1 2) (resolve '+) ;; #'clojure.core/+ Both quote forms preserve the list instead of calculating 3. Quoting also preserves the symbol so resolve can interpret it at runtime. section 4.3

Quote is deliberately primitive: it returns a constant value in O(1) time and has no performance implications. In macro code, it can also escape syntax-quote's automatic namespace qualification through the unquote-quote pattern ~'. section 4.3

How Clojure: The Essential Reference covers it

How deep it goes
Defines the one-argument contract, shorthand syntax, symbol behavior, macro escape hatch, and constant-time implementation.
Examples
Demonstrates quoted arithmetic, equivalent reader syntax, symbol resolution, and quoted macro forms passed to macroexpand.
What it assumes
Assumes familiarity with Clojure evaluation, symbols, macros, and reader syntax while defining quote's behavior directly.
What you can do after
Apply quote to preserve expressions as data and inspect or construct forms used by macros.
How it connects
Connects quote with macros, metaprogramming, macro expansion, eval, read-string, resolve, and syntax-quote.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.