The result of cons in “The Little Schemer”
-
08-07-2021 - |
题
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?
解决方案
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)
.
其他提示
(car ((quote ((a b c)))))
Try running that code snippet; you'll receive an error message. What that code does is
- Produce a list of the symbols a, b and c,
- Attempts to execute that list as a function
- 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.