2.4 Exercise 2.4
The definitions of cons and car we are given are as follows:
(define (cons x y) | (lambda (m) (m x y))) |
|
(define (car z) | (z (lambda (p q) p))) |
|
cons is a procedure of two arguments, x and y, that returns a
procedure of one argument, m, that calls m on x and y.
car is a procedure taking a procedure z as its only argument, and it
calls z on a procedure that returns the first of two arguments given to it.
We can see how this evaluates by substituting:
(car z) |
(z (lambda (p q) p)) |
((lambda (m) (m x y)) (lambda (p q) p)) |
((lambda (p q) p) x y) |
x |
We know that (car z) should return x by definition, so we can conclude
that car is correct.
We can do similarly for cdr. Its definition is almost identical to car’s,
except it passes a procedure returning its second of two arguments.
(define (cdr z) | (z (lambda (p q) q))) |
|
Using the substitution model to evaluate:
(cdr z) |
(z (lambda (p q) q)) |
((lambda (m) (m x y)) (lambda (p q) q)) |
((lambda (p q) q) x y) |
y |