The cons result in "The Little Schemer"

On page 178 there is a question: what is the meaning

(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 

Answer in the book

 (car (quote (abc))) 

But I think the answer should be

 (car ((quote ((abc))))) 

Why am I wrong?

+4
source share
2 answers

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

(quote ()) just returns an empty list. (cons 1 (quote ())) adds one item to the empty list to get (1) .

+2
source
 (car ((quote ((abc))))) 

Try executing this piece of code; You will get an error message. What this code does is

  • Create a list of characters a , b, and c ,
  • Trying to execute this list as a function
  • Applies the car to the result.

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

This should be clear when looking at source code that is not part of the code.

You probably wanted to say

 (car (quote ((abc)))) 

which is not true for the reasons given by Alex D.

0
source

Source: https://habr.com/ru/post/1440452/


All Articles