An interval is defined by an upper and lower bound You are r
An interval is defined by an upper and lower bound. You are required to write the procedures: add-interval, subtract-interval, multiply-interval, and divide-interval that add subtract multiply and divide two intervals respectively. You should create a make-interval procedure along with procedures to access the upper and lower bounds. You must deal with intervals that span zero. Addition: [a, b] + [c, d] = [a + c, b + d] Subtract: [a, b] - [c, d] = [a - d, b - c] Multiply: [a, b] * [c, d] = [min(ac, ad, bc, bd), max(ac, ad, bc, bd)] Divide: [a, b]/[c, d] = [a, b] * [1/d, 1/c] if [c, d] does not contain theta, otherwise error.
Solution
(define (add-interval x y)
(make-interval (+ (lowr-bound x) (lowr-bound y))
(+ (uppr-bound x) (uppr-bound y))))
(define (multiply-interval x y)
(let ((p1 (* (lowr-bound x) (lowr-bound y)))
(p2 (* (lowr-bound x) (uppr-bound y)))
(p3 (* (uppr-bound x) (lowr-bound y)))
(p4 (* (uppr-bound x) (uppr-bound y))))
(make-interval (min p1 p2 p3 p4)
(max p1 p2 p3 p4))))
(define (divide-interval x y)
(mul-interval x
(make-interval (/ 1.0 (uppr-bound y))
(/ 1.0 (lowr-bound y)))))
(define (make-interval a b)
(cons a b))
(define (lowr-bound interval)
(car interval))
(define (uppr-bound interval)
(cdr interval))
(define (subtract-interval x y)
(make-interval (- (lowr-bound x) (uppr-bound y))
(- (uppr-bound x) (lowr-bound y))))
