Templates using quasiquote
The quote character ' is used to indicate that the following s-expression
should be taken literally.
Without a quote the s-expression is evaluated.
(+ 3 (* 5 6))
But with the quote it is read literally
'(+ 3 (* 5 6))
Sometimes we want to write an s-expression almost literally. We would like it to be a template with some parts evaluated.
Here is a literal s-expression.
(bind ((m 100) (c 3e8)) '(e = m (* c c)))
But what if we want to put the binding for m in place of m itself?
For this we use backquote ` instead of quote ' and we put a comma , in front of
the variable we want to evaluate. The backquote is called a quasiquote.
(bind ((m 100) (c 3e8)) `(e = ,m (* c c)))
We can put the comma not only in front of a variable. We can put it in front of any s-expression we want to evaluate.
(bind ((m 100) (c 3e8)) `(e = ,m ,(* c c)))
Note that it is the comma that determines that a subexpression should be evaluated, not just the presence of a binding for a symbol.
(bind ((a 1) (b 2) (c 3e8) (d 4) (e 2.71828)) `(a b ,c d ,e))
And one last thing we might want to do is to splice a list into the middle of
an s-expression. For this we use the ,@ splicing operator.
(bind ((a 1) (b 2) (c '(speed of light)) (d 4) (e '(base of natural logarithm))) `(,a ,b ,@c ,d ,@e))
These kind of templates with backquote (`), comma (,) and splice (,@) are
particularly useful when writing Lisp macros.