Chapter 2, Building Abstractions with Data

Section - 2.1 - Introduction to Data Abstraction

Exercise 2.4


Lets first define procedure cdr and also test by invoking the procedures with an example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#lang sicp

(define (cons x y)
  (lambda (m) (m x y)))
(define (car z)
  (z (lambda (p q) p)))
(define (cdr z)
  (z (lambda (p q) q)))

(define pair (cons 3 5))
(car pair)
; 3
(cdr pair)
; 5

A we can see output is correct. Lets verify this using substitution for the invocation (cdr pair) in example above:

1
2
3
4
5
(cdr pair)
(pair (lambda (p q) q))
((lambda (m) (m 3 5))) (lambda (p q) q))
((lambda (p q) q) 3 5)
5

Similarly it can be verified for procedure cdr.