Skip to content

Instantly share code, notes, and snippets.

@yakreved
Created August 13, 2013 23:20
Show Gist options
  • Save yakreved/6226669 to your computer and use it in GitHub Desktop.
Save yakreved/6226669 to your computer and use it in GitHub Desktop.
sicp 2.12
(define (make-interval a b) (cons a b))
(define (upper-bound x) (car x))
(define (lower-bound x) (cdr x))
(define (width x) (- (cdr x) (car x)))
(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 (make-center-percent c p)
(let ((w (* c (/ p 100))))
(make-interval (- c w) (+ c w))))
(define (percent x)
(abs (* 100 (/ (- (upper-bound x) (lower-bound x)) (+ (upper-bound x) (lower-bound x)))))
)
(make-center-percent 100 5)
(percent (make-interval 95 105))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment