Next: , Up: Evaluation   [Index]


1.3.1 Evaluating More Complicated S-Expressions

Suppose we want to compute something a bit more complicated. Suppose we want to know the value of ‘8*(3+7)’. To do this computation in LISP, type the following expression:

-> (* 8 (+ 3 7))
80
->

Note that this expression looks just like the last expression we evaluated except that here the second argument is itself an expression. When LISP evaluatesan expression, it evaluates the arguments to the operator before it applies that operator to the arguments. So when LISP tries to apply ‘*’ to the second argument, it notices that this argument is itself an expression and evaluates it. The result of this evaluation is used as the argument for subsequent computation. LISP evaluates ‘(+ 3 7)’ to produce the result ‘10’. Then it multiplies the argument ‘8’ times ‘10’ to get the final result.

S-expressions, or lists, can contain elements that are themselves s-expressions, or lists. We can nest this construction as much as we like, that is, put lists inside lists inside lists, and so on. LISP will evaluate every expression according to one simple rule:

THE LIST EVALUATION RULE

Look at the outermost list first. Evaluate each of its arguments. Use the results as arguments to the outermost operator.

In this manner, we can use LISP to compute expressions of arbitrary complexity.