of objects. Some simple examples look like this:
x ; the symbol X
() ; the empty list
(1 2 3) ; a list of three numbers
('foo' 'bar') ; a list of two strings
(x y z) ; a list of three symbols
(x 1 'foo') ; a list of a symbol, a number, and a string
(+ (* 2 3) 4) ; a list of a symbol, a list, and a number.
An only slightly more complex example is the following four-item list that contains two symbols, the empty list, and another list, itself containing two symbols and a string:
(defun hello-world ()
(format t 'hello, world'))
After the reader has translated a bunch of text into s-expressions, the s-expressions can then be evaluated as Lisp code. Or some of them can—not every s-expressions that the reader can read can necessarily be evaluated as Lisp code. Common Lisp's evaluation rule defines a second level of syntax that determines which s-expressions can be treated as Lisp forms.[44] The syntactic rules at this level are quite simple. Any atom—any nonlist or the empty list—is a legal Lisp form as is any list that has a symbol as its first element.[45]
Of course, the interesting thing about Lisp forms isn't their syntax but how they're evaluated. For purposes of discussion, you can think of the evaluator as a function that takes as an argument a syntactically well-formed Lisp form and returns a value, which we can call the
The simplest Lisp forms, atoms, can be divided into two categories: symbols and everything else. A symbol, evaluated as a form, is considered the name of a variable and evaluates to the current value of the variable.[46] I'll discuss in Chapter 6 how variables get their values in the first place. You should also note that certain 'variables' are that old oxymoron of programming: 'constant variables.' For instance, the symbol PI
names a constant variable whose value is the best possible floating-point approximation to the mathematical constant
All other atoms—numbers and strings are the kinds you've seen so far—are 10
and 'hello, world'
at the REPL.
It's also possible for symbols to be self-evaluating in the sense that the variables they name can be assigned the value of the symbol itself. Two important constants that are defined this way are T
and NIL
, the canonical true and false values. I'll discuss their role as booleans in the section 'Truth, Falsehood, and Equality.'
Another class of self-evaluating symbols are the :
. When the reader interns such a name, it automatically defines a constant variable with the name and with the symbol as the value.
Things get more interesting when we consider how lists are evaluated. All legal list forms start with a symbol, but three kinds of list forms are evaluated in three quite different ways. To determine what kind of form a given list is, the evaluator must determine whether the symbol that starts the list is the name of a function, a macro, or a special operator. If the symbol hasn't been defined yet—as may be the case if you're compiling code that contains references to functions that will be defined later—it's assumed to be a function name.[47] I'll refer to the three kinds of forms as
The evaluation rule for function call forms is simple: evaluate the remaining elements of the list as Lisp forms and pass the resulting values to the named function. This rule obviously places some additional syntactic constraints on a function call form: all the elements of the list after the first must themselves be well-formed Lisp forms. In other words, the basic syntax of a function call form is as follows, where each of the arguments is itself a Lisp form:
(
Thus, the following expression is evaluated by first evaluating 1
, then evaluating 2
, and then passing the resulting values to the +
function, which returns 3:
(+ 1 2)
A more complex expression such as the following is evaluated in similar fashion except that evaluating the arguments (+ 1 2)
and (- 3 4)
entails first evaluating their arguments and applying the appropriate functions to them:
(* (+ 1 2) (- 3 4))
Eventually, the values 3 and -1 are passed to the *
function, which returns -3.
As these examples show, functions are used for many of the things that require special syntax in other languages. This helps keep Lisp's syntax regular.
That said, not all operations can be defined as functions. Because all the arguments to a function are evaluated before the function is called, there's no way to write a function that behaves like the IF
operator you used in Chapter 3. To see why, consider this form:
(if x (format t 'yes') (format t 'no'))
If IF
were a function, the evaluator would evaluate the argument expressions from left to right. The symbol x
would be evaluated as a variable yielding some value; then (format t 'yes')
would be evaluated as a function call, yielding NIL
after printing 'yes' to standard output. Then (format t 'no')
would be evaluated, printing 'no' and also yielding NIL
. Only after all three expressions were evaluated would the resulting values be passed to IF
, too late for it to control which of the two FORMAT
expressions gets evaluated.
To solve this problem, Common Lisp defines a couple dozen so-called special operators, IF
being one, that do things that functions can't do. There are 25 in all, but only a small handful are used directly in day-to-day programming.[48]
When the first element of a list is a symbol naming a special operator, the rest of the expressions are evaluated according to the rule for that operator.