-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.12.scm
25 lines (20 loc) · 923 Bytes
/
2.12.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
(load "chapters/2/2.9.scm") ; To import interval utils
; --------------------------------------------------------------------------------------------------
; Define a constructor that takes a percentage tolerance, and a selector `percent` that produces
; that same tolerance.
; --------------------------------------------------------------------------------------------------
; For simplicity, I'm treating `tolerance` as a decimal value: 10% as .1, not 10
(define (make-center-percent center tolerance)
(let ((width (abs (* center tolerance))))
(make-interval (- center width) (+ center width))))
(define (center interval)
(/ (+ (lower-bound interval) (upper-bound interval))
2))
(define (percent interval)
(/ (width interval)
(center interval)))
(define ten (make-center-percent 10 .1))
(upper-bound ten) ; => 11
(lower-bound ten) ; => 9
(center ten) ; => 10
(percent ten) ; => .1