Fork me on GitHub

基本过程:

1 (cons n d)
2 (car x)
3 (cdr x)

部分习题:

exercise 2.1

1 (define (make-rat n d)
2     (let ((g (gcd n d)))
3         (if (negative? (* n d))
4             (cons (- (abs (/ n g))) (abs (/ d g)))
5             (cons (abs (/ n g)) (abs (/ d g))))))

exercise 2.2

 1 (define (average x y)
 2     (/ (+ x y) 2))
 3 (define (make-point x y)
 4     (cons x y))
 5 (define (x-point p)
 6     (car p))
 7 (define (x-point p)
 8     (cdr p))
 9 (define (make-segment start end)
10     (cons start end))
11 (define (start-segment segment)
12     (car segment))
13 (define (end-segment segment)
14     (cdr segment))
15 
16 (define (midpoint-segment segment)
17     (let ((start-point (start-segment segment))
18           (end-point (end-segment segment)))
19         (cons (average (x-point start-point) (x-point end-point))
20               (average (y-point start-point) (y-point end-point)))))

exercise 2.4

 1 (define (cdr z)
 2     (z (lambda (p q) q)))
 3 
 4 ;代换模型
 5 (cdr (cons x y))
 6 
 7 ((lambda (m) (m x y))
 8  (lambda (p q) q))
 9 
10 ((lambda (p q) q)
11  (x y))
12 
13 y

exercise 2.5

1 (define (cons x y)
2     (define (dispatch m)
3         (cond ((= m 0) x)
4               ((= m 1) y)
5               (else (* (expt 2 x) (expt 3 y)))))
6     (dispatch))
7 (define (car z) (z 0))
8 (define (cdr z) (z 1))

exercise 2.7

假定 make-interval 的两个参数由小到大排列。

1 (define (upper-bound interval)
2     (cdr interval))
3 (define (lower-bound interval)
4     (car interval))

exercise 2.8

1 (define (sub-interval x y)
2     (let ((xl (lower-bound x))
3          (xu (upper-bound x))
4          (yl (lower-bound y))
5          (yu (upper-bound y)))
6         (make-interval (- (min xl yl) (max xl yl))
7                        (- (max xu yu) (min xu yu)))))

 

 

posted on 2012-11-03 23:09  sungoshawk  阅读(220)  评论(0编辑  收藏  举报