1.练习2.2
(define (make-segment start end)
(cons start end))
(define (start-segment seg)
(car seg))
(define (end-segment seg)
(cdr seg))
(define (make-point x y)
(cons x y))
(define (x-point p)
(car p))
(define (y-point p)
(cdr p))
(define (midpoint-segment seg)
(make-point (/ (+ (x-point (start-segment seg))
(x-point (end-segment seg))) 2)
(/ (+ (y-point (start-segment seg))
(y-point (end-segment seg))) 2)))
2.练习2.3
首次定义:
(define (make-rect start end)
(cons start end))
(define (start-rect seg)
(car seg))
(define (end-rect seg)
(cdr seg))
(define (width rect)
(abs (- (y-point (end-rect rect))
(y-point (start-rect rect)))))
(define (length rect)
(abs (- (x-point (end-rect rect))
(x-point (start-rect rect)))))
(define (girth rect)
(* (+ (width rect)
(length rect))
2))
(define (area rect)
(* (width rect)
(length rect)))
二次定义:
(define (make-rect start length width)
(cons start
(cons length width)))
(define (start-rect seg)
(car seg))
(define (end-rect seg)
(cons (+ (x-point (start-rect seg))
(car (cdr seg)))
(+ (y-point (start-rect seg))
(cdr (cdr seg)))))
3.练习2.4
(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)))
4.练习2.5
(define (cons a b)
(* (pow 2 a)
(pow 3 b)))
(define (car c)
(define (iter n result)
(if (not (= (remainder n 2) 0))
result
(iter (/ n 2) (+ result 1))))
(iter c 0))
(define (cdr c)
(define (iter n result)
(if (not (= (remainder n 3) 0))
result
(iter (/ n 3) (+ result 1))))
(iter c 0))
5.练习2.6
(define one
(lambda (f) (lambda (x) (f x))))
(define two
(lambda (f) (lambda (x) (f (f x)))))
(define (add a b)
(lambda (f) (lambda (x) ((b f) ((a f) x)))))
6.练习2.7
(define (make-interval x y)
(cons x y))
(define (upper-bound c)
(cdr c))
(define (lower-bound c)
(car c))
7.练习2.8
(define (sub-interval x y)
(let ((a (- (lower-bound x) (lower-bound y)))
(b (- (upper-bound x) (upper-bound y))))
(make-interval (min a b)
(max a b))))
8.练习2.9
(define (width-interval a)
(/ (- (upper-bound a)
(lower-bound a))
2))
9.练习2.10
(define (div-interval x y)
(if (and (<= (lower-bound y) 0) (>= (upper-bound y) 0))
(error "ERROR!!!")
(mul-interval x
(make-interval (/ 1.0 (upper-bound y))
(/ 1.0 (lower-bound y))))))