Constructing a template

Symbolic computation often involves constructing an expression out of fixed parts and variable data elements. For example:

(DEFUN makeMain (name helper identity)
(EVAL (LIST 'DEFUN name '(arglist) (LIST helper 'arglist identity)))

which constructs the expression

(DEFUN name (arglist) (helper arglist identity)

with the italicised parts filled in with data values from the arguments to makeMain.

Constructing an expression of this sort using Lisp functions can be tedious and error-prone. It would be much easier if we could lay out a template or form consisting of the fixed part of the expression

(DEFUN ________ (arglist) (____ arglist ______)

and then insert the data values into the blanks.

Lisp provides two operators--the comma and the backquote for this purpose.

The backquote operator (`) allows us to quote a data object which includes variable names; prior to evaluation of the quoted object, the values of the variables are substituted into the object. The comma operator is used to indicate which atoms in the quoted object are to be treated as variable names:

>(LET ((x '(value of x))) (print '(this is x)))

(this is x)  

>(LET ((x '(value of x))) (print `(this is ,x)))

(this is (value of x)) 

As you can see, the comma operator causes the value of x to be inserted into the back-quoted form. The comma can only be used within a backquote -- it's not an abbreviation for EVAL.

We can now rewrite our previous example in a much more straight-forward way:

(DEFUN makeMain (name helper identity)
(EVAL `(DEFUN ,name (arglist) (,helper arglist ,identity)))

Author: Peter Roosen-Runge
Last Updated: September 30, 1998