Add Exercise 2.7

This commit is contained in:
Tobias Eidelpes 2022-11-05 12:50:17 +01:00
parent 9fa67213bf
commit 329d0d7098

View File

@ -56,3 +56,32 @@
(defun print-point (p)
(format t "(~a,~a)~%" (x-point p) (y-point p)))
;; Exercise 2.7
(defun make-interval (a b)
(cons a b))
(defun lower-bound (x)
(car x))
(defun upper-bound (x)
(cdr x))
(defun add-interval (a b)
(make-interval (+ (lower-bound a) (lower-bound b))
(+ (upper-bound a) (upper-bound b))))
(defun 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))))
(defun div-interval (a b)
(mul-interval
a
(make-interval (/ 1.0 (upper-bound b))
(/ 1.0 (lower-bound b)))))