sicp-solutions/ex-2.10.scm
Petar Kapriš 1b2b2dfec1 Add solutions to exercises from section 2.1
To be noted: the last two exercises, 2.15 and 2.16 were left unfinished,
and will be left for another time.
2025-02-05 15:17:47 +01:00

25 lines
626 B
Scheme

#lang sicp
(define (make-interval a b) (cons a b))
(define (lower-bound i)
(car i))
(define (upper-bound i)
(cdr i))
(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 (div-interval x y)
(if (<= (* (lower-bound y) (upper-bound y)) 0)
(error "division by zero")
(mul-interval
x
(make-interval (/ 1.0 (upper-bound y))
(/ 1.0 (lower-bound y))))))