练习2.11-练习2.20

版权声明:本文为博主原创文章,转载请注明出处,谢谢!

版权声明:本文为博主原创文章,转载请注明出处:http://blog.jerkybible.com/2014/01/17/2014-01-17-练习2.11-练习2.20/

访问原文「练习2.11-练习2.20

1.练习2.11

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
(define (mul-interval x y)
(let ((x1 (lower-bound x))
(x2 (upper-bound x))
(y1 (lower-bound y))
(y2 (upper-bound y)))
(let
((zzx (and (>= x1 0) (>= x2 0)))
(zzy (and (>= y1 0) (>= y2 0)))
(fzx (and (< x1 0) (>= x2 0)))
(fzy (and (< y1 0) (>= y2 0)))
(ffx (and (< x1 0) (< x2 0)))
(ffy (and (< y1 0) (< y2 0))))
(cond (zzy (cond (zzx (make-interval (* x1 y1) (* x2 y2)))
(fzx (make-interval (* x1 y2) (* x2 y2)))
(ffx (make-interval (* x1 y2) (* x2 y1)))))
(fzy (cond (zzx (make-interval (* x2 y1) (* x2 y2)))
(fzx (make-interval (min (* x1 y2) (* x2 y1))
(max (* x1 y1) (* x2 y2))))
(ffx (make-interval (* x1 y2) (* x1 y1)))))
(ffy (cond (zzx (make-interval (* x2 y1) (* x1 y2)))
(fzx (make-interval (* x2 y1) (* x1 y1)))
(ffx (make-interval (* x2 y2) (* x1 y1)))))))))

2.练习2.12

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
(define (mul-interval x y)
(let ((x1 (lower-bound x))
(x2 (upper-bound x))
(y1 (lower-bound y))
(y2 (upper-bound y)))
(let
((zzx (and (>= x1 0) (>= x2 0)))
(zzy (and (>= y1 0) (>= y2 0)))
(fzx (and (< x1 0) (>= x2 0)))
(fzy (and (< y1 0) (>= y2 0)))
(ffx (and (< x1 0) (< x2 0)))
(ffy (and (< y1 0) (< y2 0))))
(cond (zzy (cond (zzx (make-interval (* x1 y1) (* x2 y2)))
(fzx (make-interval (* x1 y2) (* x2 y2)))
(ffx (make-interval (* x1 y2) (* x2 y1)))))
(fzy (cond (zzx (make-interval (* x2 y1) (* x2 y2)))
(fzx (make-interval (min (* x1 y2) (* x2 y1))
(max (* x1 y1) (* x2 y2))))
(ffx (make-interval (* x1 y2) (* x1 y1)))))
(ffy (cond (zzx (make-interval (* x2 y1) (* x1 y2)))
(fzx (make-interval (* x2 y1) (* x1 y1)))
(ffx (make-interval (* x2 y2) (* x1 y1)))))))))

3.练习2.13中文的翻译太差了,句中的“两个被乘区间的误差”应该是“被乘区间的百分比误差”吧,很生气。。。
4.练习2.14
5.练习2.15
因为引入的误差少,所以更精确
6.练习2.16
设计一种好难啊
7.练习2.17

1
2
3
4
5
6
7
(define (last-pair items)
(let ((len (length items)))
(define (last-pair-iter item tmplen)
(if (= tmplen 1)
item
(last-pair-iter (cdr item) (- tmplen 1))))
(last-pair-iter items len)))

8.练习2.18

1
2
3
4
5
6
7
(define (reverse items)
(define (reverse-iter tmpitems result)
(if (null? tmpitems)
result
(reverse-iter (cdr tmpitems)
(cons (car tmpitems) result))))
(reverse-iter items '()))

9.练习2.19

1
2
3
4
5
6
7
8
(define (first-denomination list1)
(car list1))
(define (except-first-denomination list1)
(cdr list1))
(define (no-more? list1)
(null? list1))

10.练习2.20

1
2
3
4
5
6
7
8
9
10
11
12
13
14
(define (same-parity . other)
(define (even-number? number)
(= (remainder number 2) 0))
(define (same-parity-recur even items)
(if (null? items)
'()
(let ((now-even (even-number? (car items)))
(next (same-parity-recur even (cdr items))))
(if (or (and even now-even)
(and (not even) (not now-even)))
(cons (car items) next)
next))))
(let ((evenodds (even-number? (car other))))
(same-parity-recur evenodds other)))
Jerky Lu wechat
欢迎加入微信公众号