Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 38 ยท Macros and Languages

Quote and Code as Data

Learn why Lisp code is just lists: quote, quasiquote, unquote, eval and namespaces, plus runnable JavaScript stand-ins that treat programs as nested arrays.

In Scheme and Racket, a program is written as nested lists in parentheses, and those lists are ordinary data you can build and inspect. The code (+ 1 2) is a list of three things: the symbol +, and the numbers 1 and 2. This property, called homoiconicity, is the reason macros are so natural in the Lisp family: a macro is just a function that takes a list (code) and returns a list (new code).

This lesson introduces the tools for treating code as data: quote, quasiquote and unquote, plus eval. It is the foundation for the macro lessons that follow, starting with Syntax-rules Macros.

Racket code is read-only here. The JavaScript blocks are stand-ins: they represent code as nested arrays so you can build and evaluate expressions yourself.

Quote: stop evaluation

Normally (+ 1 2) is evaluated and gives 3. Putting a quote in front says "do not evaluate, give me the structure":

RACKET
(+ 1 2)          ; => 3
(quote (+ 1 2))  ; => '(+ 1 2)   a list of three elements
'(+ 1 2)         ; the same, ' is shorthand for quote
'hello           ; => 'hello    a symbol
(car '(+ 1 2))   ; => '+
(length '(a b c)) ; => 3

A symbol is a name used as a value ('hello). When the evaluator meets an unquoted symbol it looks up a variable; when it meets a quoted one it just returns the symbol. Confusing the two is the most common beginner error: (list a b) uses the values of variables a and b, while '(a b) is a list of two symbols.

Quasiquote and unquote: templates with holes

Plain quote is too rigid when you want mostly-fixed structure with some computed pieces. Quasiquote (the backtick) acts like quote, but lets you punch holes with unquote (comma) and unquote-splicing (,@):

RACKET
(define x 10)
(define xs '(1 2 3))

`(x is ,x)             ; => '(x is 10)
`(sum ,(+ 1 2))        ; => '(sum 3)
`(list ,@xs end)       ; => '(list 1 2 3 end)
`(,x . ,xs)            ; => '(10 1 2 3)

,expr evaluates expr and inserts the result; ,@expr evaluates a list and splices its elements in place. Quasiquote is the standard way to generate code: you write the shape of the output and fill in the variable parts.

A stand-in: code as nested arrays

JavaScript has no symbols-as-code, but strings and arrays can model it. Below, an expression is a nested array whose first element is the operator, and a tiny evaluator interprets it. We also build an expression from pieces, the way quasiquote would:

JS Stand-in: building and evaluating code as data

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The important point is that generated is built by an ordinary function and only becomes a number when evaluate runs it. A Racket macro does the same: it builds the list, and the compiler evaluates it later.

Programs that rewrite programs

Since code is a list, you can write ordinary functions that transform it. Here is a stand-in that simplifies expressions by removing (* 1 x) and (+ 0 x), the sort of rewrite a compiler pass would do:

JS Stand-in: a tiny code simplifier

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Every Lisp compiler and macro system is, at heart, this kind of list-to-list function, just with a richer vocabulary.

eval and namespaces

eval takes a quoted expression and evaluates it, in the Racket world inside a namespace, which determines which names are visible:

RACKET
(define ns (make-base-namespace))
(eval '(+ 1 2) ns)                      ; => 3
(eval `(* ,(+ 2 3) 4) ns)               ; => 20

Use eval sparingly. It is slow, it defeats the optimizer, and evaluating text from untrusted input is a security hole. Macros are the right tool for generating code at compile time; eval is for REPL tools, test harnesses and embedded scripting.

Racket also has a richer form of quoted data called syntax objects (#'expr and (syntax expr)). A syntax object is code plus source locations and scope information, and it is what real Racket macros receive. The hygiene lesson explains why that extra information matters.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does '(+ 1 2) evaluate to?

    The quote prevents evaluation, so you get the list structure itself.
  2. 2. In a quasiquoted template, what does ,@xs do?

    ,@ (unquote-splicing) inserts the elements of a list, not the list itself.
  3. 3. What is (let ([x 5]) (a ,x))`?

    The comma evaluates x inside the template, so the result is the list (a 5).
  4. 4. Why is code-as-data useful for macros?

    Because code is nested lists, transforming code is just list manipulation.

Summary

  • Quote returns structure instead of evaluating; 'x is a symbol, '(a b) a list of symbols.
  • Quasiquote with , and ,@ builds lists from templates.
  • Code is nested lists, so programs can generate and rewrite programs.
  • eval evaluates data as code inside a namespace; use it sparingly.
  • Racket macros receive syntax objects, which add location and scope to quoted code.

Next: Syntax-rules Macros