From 329d0d7098ed209107a14dfa5ed93b82299dc63a Mon Sep 17 00:00:00 2001 From: Tobias Eidelpes Date: Sat, 5 Nov 2022 12:50:17 +0100 Subject: [PATCH] Add Exercise 2.7 --- chapter2.lisp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/chapter2.lisp b/chapter2.lisp index f43bac0..1c8cce4 100644 --- a/chapter2.lisp +++ b/chapter2.lisp @@ -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))))) +