next up previous
Next: 3. EXTENSION OF THE Up: 2. THE LISP INTERPRETER Previous: 2.5 Special Forms

2.6 Programming for the Interpreter

The purpose of this section is to help the programmer avoid certain common errors.

Example 1

        fn: CAR 
        args: ((A B))

The value is A. Note that the interpreter expects a list of arguments. The one argument for car is (A B). The extra pair of parentheses is necessary.

One could write (LAMBDA (X) (CAR X)) instead of just CAR. This is correct but unnecessary.

Example 2

        fn: CONS 
        args: (A (B . C))

The value is cons[A;(B . C)] = (A . (B . C)). The print program will write this as (A B . C).

Example 3

     fn: CONS 
     args: ((CAR (QUOTE (A . B))) (CDR (QUOTE (C . D))))

The value of this computation will be ((CAR (QUOTE (A . B))) . (CDR (QUOTE (C . D)))). This is not what the programmer expected. He expected (CAR (QUOTE (A . B))) to evaluate to A, and expected (A . D) as the value of cons.

The interpreter expects a list of arguments. It does not expect a list of expressions that will evaluate to the arguments. Two correct ways of writing this function are listed below. The first one makes the car and cdr part of a function specified by a LAMBDA. The second one uses quoted arguments and gets them evaluated by eval with a null a-list.

    fn: (LAMBDA (X Y) (CONS (CAR X) (CDR Y))) 
    args: ((A . B) (C . D)) 
 
    fn: EVAL 
    args: ((CONS (CAR (QUOTE (A . B))) (CDR (QUOTE (C . D)))) NIL)

The value of both of these is (A . D).


next up previous
Next: 3. EXTENSION OF THE Up: 2. THE LISP INTERPRETER Previous: 2.5 Special Forms