A backquote ` starts a template. Everything inside is quoted, except a form prefixed with a comma , (unquote), which is evaluated and its value inserted. ,@ (unquote-splicing) evaluates to a list and inserts its elements one by one instead of the list itself.
Scheme Easy · Lesson 1 · On the house
Quasiquote, unquote and splicing in depth
Build lists, dotted pairs, vectors and code templates with quasiquote; insert values with unquote and spread lists with unquote-splicing. Lesson 1 is free — on the house.
Lessons · Scheme Easy · Lesson 1 of 10 · On the house
Quasiquote, unquote and splicing in depth
Build lists, dotted pairs, vectors and code templates with quasiquote; insert values with unquote and spread lists with unquote-splicing.

Splicing an empty list inserts nothing, so `(1 ,@'() 2) is (1 2). A spliced value must be a proper list. Unquote after a dot fills the tail: `(head . ,xs) gives (head a b c) when xs is (a b c).
Templates work inside vectors too: `#(1 ,x ,(* x x)) builds a vector. They also build alists such as `((age . ,(+ 30 1))), a common way to make records for JSON-like data.
The reader turns the shorthand into ordinary lists: `x is (quasiquote x), ,x is (unquote x) and ,@x is (unquote-splicing x). write on a quoted template shows these long forms.
Because code is data, quasiquote is the easy way to generate code. make-adder-code returns the list (lambda (y) (+ y 10)). (eval expr (interaction-environment)) compiles and runs such a list in the current top-level environment.
Recursive templates build nested code: power-code unrolls x to the nth power at generation time. Mapping a template over names, as in bindings, produces the binding list for a generated let.
Quasiquote is only list building. It knows nothing about scope, so a generated symbol can clash with a user's variable. That problem is what hygienic macros, in the next lesson, solve.
quasiquote
(define x 5)
(define xs '(a b c))
(display `(x is ,x)) (newline)
(display `(list ,@xs end)) (newline)
(display `(1 ,@'() 2)) (newline)
(display `(head . ,xs)) (newline)
(display `#(1 ,x ,(* x x))) (newline)
(display `((name . ,'ann) (age . ,(+ 30 1)))) (newline)
(write '`(a ,b ,@c)) (newline)
(define (make-adder-code n) `(lambda (y) (+ y ,n)))
(write (make-adder-code 10)) (newline)
(display ((eval (make-adder-code 10) (interaction-environment)) 5)) (newline)
(define (power-code n)
(if (= n 0) 1 `(* x ,(power-code (- n 1)))))
(write (power-code 3)) (newline)
(define (bindings names) (map (lambda (n) `(,n 0)) names))
(write `(let ,(bindings '(a b)) (list a b))) (newline)
Quiz
Quiz
Quiz
Quiz
Check
Match the still for: Quasiquote, unquote and splicing in depth.
Open-book: the answers are on this page. Pass every quiz and check (5) to mark the lesson done. This visit: 0/5.