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.

Illustration for Scheme
Lesson 1 is on the house.

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.

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)
Match this when you type.

Quiz

With xs bound to (a b c), what does `(list ,@xs end) produce?

Quiz

What does `(1 ,@'() 2) produce?

Quiz

What does the reader turn ,x into?

Quiz

Which call runs a generated code list in the current top-level environment?

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.