-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.9.rkt
More file actions
57 lines (54 loc) · 1.56 KB
/
2.9.rkt
File metadata and controls
57 lines (54 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#lang scheme
; 预定义函数
(define (make-interval a b) (cons a b))
(define (lower-bound z)
(min (car z)
(cdr z)))
(define (upper-bound z)
(max (car z)
(cdr z)))
(define (add-interval x y)
(make-interval (+ (lower-bound x) (lower-bound y))
(+ (upper-bound x) (upper-bound y))))
(define (sub-interval x y)
(make-interval (- (lower-bound x) (upper-bound y))
(- (upper-bound x) (lower-bound y))))
; get width of intervel
(define (width-of-interval x)
(/ (- (upper-bound x)
(lower-bound x))
2))
; 推导出 x y 和的 width 仅仅与 x y 的 width 有关:
;(width-of-interval (add-interval x y))
; =>
;(/ (- (upper-bound (add-interval x y))
; (lower-bound (add-interval x y)))
; 2)
; 其中 (add-interval x y) 可以展开为:
; (make-interval (+ (lower-bound x) (lower-bound y))
; (+ (upper-bound x) (upper-bound y)))
; =>
;(/ (- (+ (upper-bound x) (upper-bound y)))
; (+ (lower-bound x) (lower-bound y))
; 2)
; =>
;(/ (+ (- (upper-bound x) (lower-bound x))
; (- (upper-bound y) (lower-bound y)))
; 2)
; =>
;(/ (+ (* (width-of-interval x) 2)
; (* (width-of-interval y) 2))
; 2)
; =>
;(+ (width-of-interval x)
; (width-of-interval y))
; 由以上可以得到
(define (width-of-sum-interval x y)
(+ (width-of-interval x)
(width-of-interval y)))
; 测试
(define x (make-interval 4 9))
(define y (make-interval 2 6))
(width-of-sum-interval x y)
(width-of-interval (add-interval x y))
; 证明乘法不适用这个,略。简单讲就是乘法和基数有关系。