SICP Exercise 2.12
Question
Define a constructor make-centre-percent
that takes a centre
and a percentage tolerance and produces the desired interval. You
must also define a selector percent
that produces the
percentage tolerance for a given interval. The centre
selector
is the same as the one shown above.
Answer
(define (make-centre-percent c p)
(let ((w (* c (/ p 100))))
(make-centre-width c w)))
(define (percent i)
(let ((w (width i)))
(* (/ w (centre i)) 100)))
Let’s test it:
(define i (make-centre-percent 50 20))
(percent i)
Results:
20