Concepts of Programming Languages

(Sean Pound) #1
15.6 Common LISP 699

((NULL? a_list) 0)
(ELSE (+ (CAR a_list) (adder (CDR a_list))))
))

Following is an example call to adder, along with the recursive calls and
returns:

(adder '(3 4 5))
(+ 3 (adder (4 5)))
(+ 3 (+ 4 (adder (5))))
(+ 3 (+ 4 (+ 5 (adder ()))))
(+ 3 (+ 4 (+ 5 0)))
(+ 3 (+ 4 5))
(+ 3 9)
(12)

An alternative solution to the problem is to write a function that builds
a call to + with the proper parameter forms. This can be done by using CONS
to build a new list that is identical to the parameter list except it has the atom
+ inserted at its beginning. This new list can then be submitted to EVAL for
evaluation, as in the following:

(DEFINE (adder a_list)
(COND
((NULL? a_list) 0)
(ELSE (EVAL (CONS '+ a_list)))
))

Note that the + function’s name is quoted to prevent EVAL from evaluating it
in the evaluation of CONS. Following is an example call to this new version of
adder, along with the call to EVAL and the return value:

(adder '(3 4 5))
(EVAL (+ 3 4 5)
(12)

In all earlier versions of Scheme, the EVAL function evaluated its expression
in the outermost scope of the program. The later versions of Scheme, beginning
with Scheme 4, requires a second parameter to EVAL that specifies the scope in
which the expression is to be evaluated. For simplicity’s sake, we left the scope
parameter out of our example, and we do not discuss scope names here.

15.6 Common LISP


Common LISP (Steele, 1990) was created in an effort to combine the features
of several early 1980s dialects of LISP, including Scheme, into a single lan-
guage. Being something of a union of languages, it is quite large and complex,
Free download pdf