Scheme quasiquote
last modified November 24, 2026 Guile 3.0
Quasiquote builds a list from a template. Most of the template is
taken literally, as with quote, but marked parts are evaluated
and their values are inserted. It is written with a backquote
`, and the marks are a comma , for a single value
and ,@ for the elements of a list. Quasiquote makes it easy to
build nested data and Scheme code, and it is used by many
macros. The
examples run with GNU Guile 3.0.7.
Quote and quasiquote
A quasiquoted template without commas is the same as a quoted one. A comma before an expression, called unquote, evaluates the expression and puts its value into the result.
(define city "Nitra") (define population 76000) (write '(city population)) (newline) (write `(city population)) (newline) (write `(city ,city population ,population)) (newline) (write `(capital? ,(string=? city "Bratislava"))) (newline) (write (list 'city city 'population population)) (newline)
The first two lines produce the same list of two symbols. In the third,
,city and ,population are replaced by the values of
the variables, while the symbols without a comma stay as they are. Any
expression can follow the comma, such as the comparison in the fourth line.
The last line builds the same list with list and quoted symbols,
which is longer and harder to read for larger templates.
$ guile basics.scm (city population) (city population) (city "Nitra" population 76000) (capital? #f) (city "Nitra" population 76000)
Inserting values
An unquoted expression can appear anywhere in the template, at any depth of nesting. This makes quasiquote convenient for procedures that return structured data.
(define (square x) (* x x))
(write `(square of 7 is ,(square 7)))
(newline)
(define (make-point x y)
`(point (x ,x) (y ,y)))
(write (make-point 3 -2))
(newline)
(define (event name hour minute)
`(event ,name
(time ,hour ,minute)
(late? ,(> hour 20))
(label ,(string-append name " at " (number->string hour)))))
(write (event "concert" 21 30))
(newline)
(define items (list "tea" "milk"))
(write `(shopping ,items (count ,(length items))))
(newline)
(write `(first ,(car items) rest ,(cdr items)))
(newline)
The first template inserts the result of a call. make-point
returns a nested list with the coordinates in their places, and
event fills four places at different levels, including a boolean
and a string built with string-append. When the value of an
unquoted expression is a list, it is inserted as one element: the shopping
list appears as a nested list, and so does the cdr of
items.
$ guile unquote.scm
(square of 7 is 49)
(point (x 3) (y -2))
(event "concert" (time 21 30) (late? #t) (label "concert at 21"))
(shopping ("tea" "milk") (count 2))
(first "tea" rest ("milk"))
Splicing lists
,@, called unquote-splicing, evaluates an expression
that must return a list and inserts the elements of that list instead of the
list itself.
(define fruits (list 'apple 'pear 'plum)) (define empty '()) (write `(basket ,fruits)) (newline) (write `(basket ,@fruits)) (newline) (write `(basket ,@fruits and ,@(list 'fig 'kiwi))) (newline) (write `(basket ,@empty)) (newline) (define (wrap tag . children) `(,tag ,@children)) (write (wrap 'ul (wrap 'li "one") (wrap 'li "two"))) (newline) (define (numbered xs) `(total ,(length xs) ,@(map (lambda (x) (* x 10)) xs))) (write (numbered (list 1 2 3))) (newline) (write `(,@fruits ,@fruits)) (newline)
With a comma, the fruits form one nested list; with ,@ they
become elements of the basket. Several splices can be combined, and splicing
the empty list inserts nothing. wrap puts a tag in front of any
number of children, building a small tree. numbered splices the
result of map after a count. A splice may also stand first in a
template, and the same list may be spliced twice.
$ guile splicing.scm (basket (apple pear plum)) (basket apple pear plum) (basket apple pear plum and fig kiwi) (basket) (ul (li "one") (li "two")) (total 3 10 20 30) (apple pear plum apple pear plum)
The long forms
The backquote and the commas are abbreviations, just as 'x is
short for (quote x). The reader turns `x into
(quasiquote x), ,x into (unquote x),
and ,@x into (unquote-splicing x).
(define n 4) (define xs (list 1 2)) (write '`(a ,n ,@xs)) (newline) (write (quasiquote (a (unquote n) (unquote-splicing xs)))) (newline) (write `(a ,n ,@xs)) (newline) (define form '`(b ,n)) (write (car form)) (newline) (write (car (cadr (cadr form)))) (newline) (define tail (list 3 4)) (define built `(1 2 ,@tail)) (write built) (newline) (write (eq? (cddr built) tail)) (newline)
Quoting a quasiquoted template shows the long forms that the reader produced.
Writing the long forms by hand gives the same result as the short syntax. The
template is an ordinary list, so its parts can be taken apart with
car and cadr. The last lines show that the result
may share structure with the spliced list: a splice at the end of a template
is not copied, so (cddr built) is the same object as
tail. Modifying one would modify the other.
$ guile longform.scm (quasiquote (a (unquote n) (unquote-splicing xs))) (a 4 1 2) (a 4 1 2) quasiquote unquote (1 2 3 4) #t
Dotted pairs and association lists
Templates may contain dotted pairs. An unquote after the dot fills the
cdr of a pair, which is just what an
association list needs.
(define (make-user name age admin)
`((name . ,name)
(age . ,age)
(admin . ,admin)
(created . 2026)))
(define user (make-user "Lucia" 29 #f))
(write user)
(newline)
(write (cdr (assq 'age user)))
(newline)
(define defaults '((port . 8080) (host . "localhost")))
(define (config port)
`((port . ,port) ,@defaults))
(write (config 9000))
(newline)
(write (cdr (assq 'port (config 9000))))
(newline)
(write `(1 . ,(+ 1 1)))
(newline)
(write `(1 2 . ,(list 3 4)))
(newline)
make-user builds an association list from a template in which
the keys and the constant entry are literal and the values are inserted.
config puts a new port in front of the default entries by
splicing them; assq finds the first matching key, so the new
port shadows the default one. A value after the dot becomes the
cdr of the last pair: when that value is a list, the result is a
proper list.
$ guile alists.scm ((name . "Lucia") (age . 29) (admin . #f) (created . 2026)) 29 ((port . 9000) (port . 8080) (host . "localhost")) 9000 (1 . 2) (1 2 3 4)
Vector templates
Quasiquote also works on vector literals.
`#(...) builds a new vector in which the unquoted parts are
evaluated and spliced.
(define x 10) (define ys (list 20 30)) (write `#(x ,x)) (newline) (write `#(,x ,@ys 40)) (newline) (write (vector? `#(,x))) (newline) (define (row label . cells) `#(,label ,@cells ,(apply + cells))) (write (row 'q1 4 5 6)) (newline) (write (row 'q2 7 8 9)) (newline) (write `(matrix #(1 ,x) #(,@ys))) (newline)
Without a comma, x stays a symbol inside the vector. Unquote and
splicing work as in lists, and the result is a real vector, newly created on
each evaluation. row builds a table row with a label, the
cells, and their sum. Vector templates can also be nested inside a list
template.
$ guile vectors.scm #(x 10) #(10 20 30 40) #t #(q1 4 5 6 15) #(q2 7 8 9 24) (matrix #(1 10) #(20 30))
Splicing a value that is not a list
The expression after ,@ must return a list. Guile implements
splicing with append, so the result for other values depends on
the position of the splice.
(define size 5) (define parts (list 1 2)) (write `(1 ,@parts)) (newline) (write `(1 ,@size)) (newline) (write `(1 ,@size 2)) (newline)
Splicing the list parts works as expected. Splicing the number
5 at the end of a template gives a dotted pair, because the last argument of
append may be any value. In the middle of a template, the number
would have to be followed by more elements, and append fails with
a wrong type error. The backtrace is omitted.
$ guile splicefail.scm (1 1 2) (1 . 5) ERROR: In procedure append: In procedure append: Wrong type argument in position 1 (expecting empty list): 5
Generating code
Scheme code is made of lists, so quasiquote can build code as easily as
data. The expressions and evaluation
article ran a quoted list with eval; with quasiquote, parts of
the code can be computed first.
(define (make-sum numbers) `(+ ,@numbers)) (define expr (make-sum (list 4 8 15 16 23 42))) (write expr) (newline) (write (eval expr (interaction-environment))) (newline) (define (make-adder n) `(lambda (x) (+ x ,n))) (write (make-adder 5)) (newline) (define add5 (eval (make-adder 5) (interaction-environment))) (write (add5 10)) (newline) (define (make-test var low high) `(and (>= ,var ,low) (<= ,var ,high))) (define test (make-test 'temp 18 24)) (write test) (newline) (write (eval `(let ((temp 21)) ,test) (interaction-environment))) (newline)
make-sum splices the numbers into a call to +, and
eval runs it. make-adder returns the code of a
procedure with the number n built in; evaluating that code
creates a real procedure. make-test builds a range test for a
variable given as a symbol, and the last eval wraps the test in
a let that binds the variable. Generating code at run time is
rarely needed in programs, but Lisp-style macros written with Guile's
define-macro use exactly this technique
at expansion time.
$ guile code.scm (+ 4 8 15 16 23 42) 108 (lambda (x) (+ x 5)) 15 (and (>= temp 18) (<= temp 24)) #t
Templates for documents
Nested lists can describe a document tree, with a symbol for each element and its contents after it. Quasiquote fills such a template with data, and a short procedure turns the tree into HTML text.
(define (render node)
(cond ((string? node) node)
((number? node) (number->string node))
(else
(let ((tag (symbol->string (car node))))
(string-append "<" tag ">"
(apply string-append (map render (cdr node)))
"</" tag ">")))))
(define (item-row name price)
`(tr (td ,name) (td ,price)))
(define (price-table title items)
`(div
(h2 ,title)
(table
,@(map (lambda (item) (item-row (car item) (cdr item))) items))))
(define menu
(price-table "Menu" (list (cons "Soup" 4) (cons "Goulash" 11))))
(write menu)
(newline)
(display (render menu))
(newline)
render converts strings and numbers to text and every other node
to an element whose tag is the first symbol of the list.
item-row builds a table row from a template, and
price-table splices the rows produced by map into
the table. The tree is easy to inspect with write before it is
rendered. A real HTML generator would also escape characters such as
< in the text.
$ guile templates.scm (div (h2 "Menu") (table (tr (td "Soup") (td 4)) (tr (td "Goulash") (td 11)))) <div><h2>Menu</h2><table><tr><td>Soup</td><td>4</td></tr><tr><td>Goulash</td><td>11</td></tr></table></div>
Nested quasiquotes
A quasiquote inside another quasiquote starts a new level. A comma belongs to the innermost backquote, so an unquote is evaluated only when it is at the same level as the outer template. This matters for code that writes code which itself uses quasiquote.
(define x 1)
(write `(a ,x `(b ,x)))
(newline)
(write `(a `(b ,(c ,x))))
(newline)
(define (make-printer name)
`(define (,(symbol-append 'print- name) value)
(display ',name)
(display ": ")
(display value)
(newline)))
(define code (make-printer 'size))
(write code)
(newline)
(eval code (interaction-environment))
(eval '(print-size 42) (interaction-environment))
In the first template, the outer ,x is replaced by 1, while the
inner ,x belongs to the inner quasiquote and stays as written.
In the second, ,(c ,x) lowers the level once and ,x
lowers it again, so x is evaluated. make-printer
generates the definition of a procedure whose name is built with
symbol-append. The template contains ',name, a quote
around an unquote, so the generated code displays the symbol itself instead
of a variable called size. Evaluating the definition creates
print-size, which the last line calls.
$ guile nested.scm (a 1 (quasiquote (b (unquote x)))) (a (quasiquote (b (unquote (c 1))))) (define (print-size value) (display (quote size)) (display ": ") (display value) (newline)) size: 42
Source
Expression Syntax — GNU Guile Reference Manual
Revised7 Report on the Algorithmic Language Scheme
In this article we have built lists from quasiquote templates, inserted
values with unquote, spliced lists with unquote-splicing, looked at the long
forms behind the short syntax, built association lists and vectors from
templates, seen what happens when a splice is not a list, generated and
evaluated code, rendered a document template as HTML, and nested
quasiquotes. The expressions and
evaluation article explains quote and eval, and
the pairs and lists article covers the lists that
quasiquote builds.
Author
List all Scheme tutorials.