分析题目,lower-bound和upper-bound选择过程分别是(car z)、(cdr z)的最小值和最大值,过程如下
#lang racket
(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 (mul-interval x y)
(let ((p1 (* (lower-bound x) (lower-bound y)))
(p2 (* (lower-bound x) (upper-bound y)))
(p3 (* (upper-bound x) (lower-bound y)))
(p4 (* (upper-bound x) (upper-bound y))))
(make-interval (min p1 p2 p3 p4)
(max p1 p2 p3 p4))))
(define (div-interval x y)
(mul-interval x (make-interval (/ 1.0 (lower-bound y)) (/ 1.0 (upper-bound y)))))
(add-interval (make-interval 1 2) (make-interval 2 7))
(mul-interval (make-interval 1 2) (make-interval 2 7))
(div-interval (make-interval 1 2) (make-interval 2 7))
(mul-interval (make-interval -1 2) (make-interval -2 7))
运行结果
'(3 . 9)
'(2 . 14)
'(0.14285714285714285 . 1.0)
'(-7 . 14)