(define (make-
interval x y)
(cons x y))
(define (add-
interval x y)
(make-interval (+ (lower-bound x) (lower-
bound y))
(+ (upper-bound x) (upper-
bound y))))
;;;;;;;;;;;;;;;2.7
(define (upper-
bound inter)
(cdr inter))
(define (lower-
bound inter)
(car inter))
;;;;;;;;;;;;;;2.8
(define (sub-
interval x y)
(add-
interval x
(make-interval (- (upper-
bound y))
(- (lower-
bound y)))))
;;;;;;;;;;;;;;2.9
(define (width-
interval x)
(/ (- (upper-bound x) (lower-bound x))
2))
;(width-interval (add-interval x y)) (
1)
;(width-interval (sub-interval x y)) (
2)
;(+ (width-interval x) (width-interval y)) (
3)
;(- (width-interval x) (width-interval y)) (
4)
;
;(1) (
3)是等价的
;(width-interval (mul-interval x y)) (
5)
;(* (width-interval x) (width-interval y)) (
6)
;(5) (
6) 不等价
;;;;;;;;;;;2.10
(define (div-
interval x y)
(if (< (* (upper-bound y) (lower-bound y))
0)
(error "division interval isn't right")
(mul-
interval x
(make-interval (/
1.0 (upper-
bound y))
(/
1.0 (lower-
bound y))))))
;;;;;;;;;2.11
(define (mul-
interval x y)
(let ((xl (lower-
bound x))
(xu (upper-
bound x))
(yu (upper-
bound y))
(yl (lower-
bound y)))
(cond ((and (< xl
0) (< xu
0)) (cond ((and (< yl
0) (< yu
0))
(make-
interval
(* xu yu) (*
xl yl)))
((and (< yl
0) (> yu
0))
(make-
interval
(* xl yu) (*
xl yl)))
((and (> yl
0) (> yu
0))
(make-
interval
(* xl yu) (*
xu yl)))))
((and (< xl
0) (> xu
0)) (cond ((and (< yl
0) (< yu
0))
(make-
interval
(* xu yl) (*
xl yl)))
((and (< yl
0) (> yu
0))
(make-
interval
(* xl yu) (*
xu yu)))
((and (> yl
0) (> yu
0))
(make-
interval
(* xl yu) (*
xu yu)))))
((and (> xl
0) (> xu
0)) (cond ((and (< yl
0) (< yu
0))
(make-
interval
(* xu yl) (*
xl yu)))
((and (< yl
0) (> yu
0))
(make-
interval
(* xu yl) (*
xu yu)))
((and (> yl
0) (> yu
0)) (make-interval
(* xl yl) (*
xu yu)))))))
;;;;;;;;;;;;;;;改成这种形式可能是因为当乘法代价比较高时
;;;;;;;;;;;;;;;可以提高效率
转载于:https://www.cnblogs.com/tclan126/p/6379366.html
相关资源:数据结构—成绩单生成器