
Quoting And Evaluation
Quote
When an expression is typed at the Tom's Lisp prompt, the program
reads it in and evaluates it. Constants evaluate to themselves,
identifiers evaluate to the values given by set, and lists
are evaluated as calls. For the lists, the
parameters are generally evaluated first,
and the resulting values become the arguments.
When we want to prevent something from being evaluated, we can
use the tick mark (') to prevent it, as in:
lsp>(car '(a b c))
a
lsp>'(car (a b c))
(car (a b c))
lsp>(car (a b c))
**** Error 1: Undefined a ****
(car (a b c))
(a b c)
The tick is actually a short-hand for the quote operator:
lsp>(quote (car (a b c)))
(car (a b c))
lsp>(car (quote (a b c)))
a
The Tom's
Lisp input function actually substitutes the quote (and the
closing paren) when you type a tick.
lsp>'(car '(a b c)))
(car (quote (a b c)))
Eval
The opposite of quote is eval, which takes a Tom's Lisp
expression and evaluates it.
lsp>(eval '(car '(a b c))))
a
This is useful when you want to evaluate something which is the
result of another computation, rather than something you just typed in.
lsp>(eval (cons '+ (cdr '(4 9 12 3 7))))
31
We will find more use for this when we have a few more pieces.
Apply
The apply function takes a function and an argument list, and
runs the function on the argument list. The argument list is
evaluated once in sending to apply, then each argument is
evaluated again when the function is run with the argument list.
lsp>(apply + '(4 9 8 2))
23
lsp>(apply car '((hi there alice)))
hi
lsp>(apply cons '(a b))
(a . b)
These are just fancy ways of writing, respectively, (+ 4 9 8 2),
(car '(hi there alice)), and (cons 'a 'b). More
precisely, the first one is (+ '4 '9 '8 '2), which means
the same thing. In each case, the function is just added to the
front, and the list is evaluated.