-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_8.scm
29 lines (22 loc) · 876 Bytes
/
2_8.scm
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
#lang sicp
(define (make-interval a b) (cons a b))
(define (upper-bound interval)
(max (car interval) (cdr interval)))
(define (lower-bound interval)
(min (car interval) (cdr interval)))
(define (add-interval a b)
(make-interval (+ (lower-bound a) (lower-bound b))
(+ (upper-bound a) (upper-bound b))))
(define (mul-interval a b)
(let ((p1 (* (lower-bound a) (lower-bound b)))
(p2 (* (lower-bound a) (upper-bound b)))
(p3 (* (upper-bound a) (lower-bound b)))
(p4 (* (upper-bound a) (upper-bound b))))
(make-interval (min p1 p2 p3 p4)
(max p1 p2 p3 p4))))
(define (div-interval a b)
(mul-interval a
(make-interval (/ 1 (upper-bound b)) (/ 1 (lower-bound b)))))
(define (sub-interval a b)
(add-interval a
(make-interval (- (upper-bound b)) (- (lower-bound b)))))