typestar

Backquote templates in Lisp

Backquote builds a list template; comma and comma-at fill in the holes.

;; Backquote builds list templates; comma and comma-at fill the holes.

(defvar *host* "db-01")
(defvar *ports* (list 5432 5433))

;; Plain quote keeps every symbol literal, holes and all.
(defvar *literal* '(connect host ports))

;; Comma evaluates one form, comma-at splices a whole list into place.
(defvar *filled* `(connect ,*host* ,@*ports*))

;; Templates nest, so one backquoted form can lay out a whole plan.
(defvar *plan* `((step check ,*host*) (step open ,@*ports*)))

(format t "literal: ~s~%filled: ~s~%plan: ~s~%" *literal* *filled* *plan*)

How it works

  1. '(connect host ports) keeps every symbol literal, so nothing is evaluated.
  2. After the backquote, ,*host* inserts one value into (connect ,*host* ,@*ports*) and ,@*ports* splices a whole list.
  3. Templates nest, so *plan* builds a two-step list of lists from the same two variables.

Keywords and builtins used here

The run, in numbers

Lines
15
Characters to type
555
Tokens
67
Three-star pace
65 tpm

At the three-star pace of 65 tokens a minute, this run takes about 62 seconds.

Type this snippet

Step 3 of 3 in Format & macros, step 24 of 27 in Language basics.

← Previous Next →