Skip to content

Instantly share code, notes, and snippets.

@tom-lpsd
Created January 8, 2011 02:33
Show Gist options
  • Save tom-lpsd/770473 to your computer and use it in GitHub Desktop.
Save tom-lpsd/770473 to your computer and use it in GitHub Desktop.
answer for sicp question 2.11
(define (make-interval a b) (cons a b))
(define (upper-bound r) (max (car r) (cdr r)))
(define (lower-bound r) (min (car r) (cdr r)))
(define (mul-interval x y)
(define (cross-zero? a b)
(and (<= a 0) (>= b 0)))
(let ((a (lower-bound x))
(b (upper-bound x))
(c (lower-bound y))
(d (upper-bound y)))
(cond ((cross-zero? a b)
(cond ((cross-zero? c d)
(make-interval (min (* b c) (* a d))
(max (* a c) (* b d))))
(else
(let ((factor (if (< c 0) c d)))
(make-interval (* a factor) (* b factor))))))
((cross-zero? c d)
(let ((factor (if (< a 0) a b)))
(make-interval (* factor c) (* factor d))))
((< (* a c) 0)
(make-interval (* a d) (* b c)))
(else
(make-interval (* a c) (* b d))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment