Question

On page 178, there is a question: what is the value of

(cons rep-car
    (cons (cons rep-quote
        (cons
            (cons rep-a
                (cons rep-b
                    (cons rep-c
                        (quote ()))))
            (quote ())))
    (quote ())))

where

rep-car is car
rep-quote is quote
rep-a is a
rep-b is b
rep-c is c

The answer in the book is

(car (quote (a b c)))

But I think the answer should be

(car ((quote ((a b c)))))

Why am I wrong?

Was it helpful?

Solution

No, the answer in the book is right. Note that the expression has 3 occurrences of (quote ()), to create 3 lists. Then it conses various atoms onto the lists. Your answer contains 5 lists, not 3.

(quote ()) simply returns an empty list. (cons 1 (quote ())) adds one item to the empty list to yield (1).

OTHER TIPS

(car ((quote ((a b c)))))

Try running that code snippet; you'll receive an error message. What that code does is

  1. Produce a list of the symbols a, b and c,
  2. Attempts to execute that list as a function
  3. Applies car to the result.

Since step 2 will fail (because '(a b c) is not a function), step 3 is never reached.

It should be clear from looking at the original code that no part of the code does that.

You probably meant to say

(car (quote ((a b c))))

which is wrong for the reasons given by Alex D.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top