SICP 2.3.1 Quotation
2023-07-05 Wed

In order to manipulate symbols we need a new element in our language: the ability to quote a data object.

(list a b) constructs a list of the values of a and b, not the list of a and b themselves (the symbols).

As analytic philosophy students well know,

[t]his issue is well known in the context of natural languages, where words and sentences may be regarded either as semantic entities or as character strings (syntactic entities)…

(In fact, footnote 98 uses an example from Frege.)

We can follow this same practice to identify lists and symbols that are to be treated as data objects rather than as expressions to be evaluated.

Ex. 2.53

Exercise:

What would the interpreter print in response to evaluating each of the following expressions?

(list 'a 'b 'c)
(list (list 'george))
(cdr '((x1 x2) (y1 y2)))
(cadr '((x1 x2) (y1 y2)))
(pair? (car '(a short list)))
(memq 'red '((red shoes) (blue socks)))
(memq 'red '(red shoes blue socks))

Answer:

(list 'a 'b 'c)
;; => (a b c)
(list (list 'george))
;; => ((george))
(cdr '((x1 x2) (y1 y2)))
;; => ((y1 y2))
(cadr '((x1 x2) (y1 y2)))
;; => (y1 y2)
(pair? (car '(a short list)))
;; => #f
(memq 'red '((red shoes) (blue socks)))
;; => #f
(memq 'red '(red shoes blue socks))
;; => (red shoes blue socks)

Ex. 2.54

Exercise:

Two lists are said to be equal? if they contain equal elements arranged in the same order. For example,

(equal? '(this is a list) 
        '(this is a list))

is true, but

(equal? '(this is a list) 
        '(this (is a) list))

is false. To be more precise, we can define equal? recursively in terms of the basic eq? equality of symbols by saying that a and b are equal? if they are both symbols and the symbols are eq?, or if they are both lists such that (car a) is equal? to (car b) and (cdr a) is equal? to (cdr b). Using this idea, implement equal? as a procedure.

Answer:

(define (equal? a b)
  (cond ((and (not (pair? a)) (not (pair? b)) (eq? a b))
         true)
        ((and (and (pair? a) (pair? b))
              (equal? (car a) (car b))
              (equal? (cdr a) (cdr b)))
         true)
        (else false)))

Ex. 2.55

Exercise:

Eva Lu Ator types to the interpreter the expression

(car ''abracadabra)

To her surprise, the interpreter prints back quote. Explain.

Answer:

From the interpreter's perspective, 'abracadabra is actually (quote abracadabra) (see footnote 100). And ''abracadabra is actually (quote (quote abracadabra)). The car of the latter is indeed quote.

Send me an email for comments.

Created with Emacs 29.0.91 (Org mode 9.6.6)