Eva (and W.Z.) is right. We need to distinguish between dependent and independent intervals. In the formula for resistance and
are independent intervals (they correspond to resistance of physically different resistors), and in procedure par1 two occurrences of
are dependent intervals. When we calculate the ratio
we cannot suppose that those two intervals
are independent, we cannot choose their values independently. If we randomly choose the value inside the interval
, we must assign this value to both occurrences of
in the formula. For independent intervals the situation is different, we may choose one value inside
, some other value inside
, and their ratio will have meaning, namely this will be some value inside the interval which is the ratio of two intervals,
. Alyssa’s package treats all occurrences of the same interval in a complex formula as independent intervals and the answers are wrong. The calculation of
by Alyssa’s package gives wrong result.
SICP Exercise 2.14
Code
#lang racket ; v1 (require "ex2.7.scm") (require "ex2.10.scm") (require "ex2.12.scm") (define A (make-center-percent 100 0.01)) (define R (div-interval A A)) R (width R) (tolerance R)
Output
'(0.9801980198019802 . 1.0202020202020203) 0.020002000200020076 0.019998000199980076
SICP Exercise 2.12
Code
#lang racket
; v1
(require "ex2.7.scm")
(provide make-center-width)
(provide center)
(provide width)
(provide make-center-percent)
(provide tolerance)
(define (make-center-width c w)
(make-interval (- c w) (+ c w)))
(define (center i)
(/ (+ (lower-bound i) (upper-bound i)) 2))
(define (width i)
(/ (- (upper-bound i) (lower-bound i)) 2))
; tol: 0.1 means 10%
(define (make-center-percent c t)
(make-center-width c (* c t)))
(define (tolerance i)
(define c (center i))
(if (= c 0)
(error "Interval with center at zero has no tolerance")
(/ (width i) c)))
SICP Exercise 2.11
Code
#lang racket
; v1
(define make-interval cons)
(define lower-bound car)
(define upper-bound cdr)
(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 (new-mul-interval x y)
(define x1 (lower-bound x))
(define x2 (upper-bound x))
(define y1 (lower-bound y))
(define y2 (upper-bound y))
(cond
((and (<= 0 x1) (<= x1 x2) (<= 0 y1) (<= y1 y2)) (make-interval (* x1 y1) (* x2 y2)))
((and (<= x1 0) (<= 0 x2) (<= 0 y1) (<= y1 y2)) (make-interval (* x1 y2) (* x2 y2)))
((and (<= x1 x2) (<= x2 0) (<= 0 y1) (<= y1 y2)) (make-interval (* x1 y2) (* x2 y1)))
((and (<= 0 x1) (<= x1 x2) (<= y1 0) (<= 0 y2)) (make-interval (* x2 y1) (* x2 y2)))
((and (<= x1 x2) (<= x2 0) (<= y1 0) (<= 0 y2)) (make-interval (* x1 y2) (* x1 y1)))
((and (<= 0 x1) (<= x1 x2) (<= y1 y2) (<= y2 0)) (make-interval (* x2 y1) (* x1 y2)))
((and (<= x1 0) (<= 0 x2) (<= y1 y2) (<= y2 0)) (make-interval (* x2 y1) (* x1 y1)))
((and (<= x1 x2) (<= x2 0) (<= y1 y2) (<= y2 0)) (make-interval (* x2 y2) (* x1 y1)))
;((and (<= x1 0) (<= 0 x2) (<= y1 0) (<= 0 y2)) (mul-interval x y))
(else (mul-interval x y))))
(define (equal-intervals? r1 r2)
(and
(= (lower-bound r1)
(lower-bound r2))
(= (upper-bound r1)
(upper-bound r2))))
; tests
(define (same-mul? i1 i2)
(define r1 (new-mul-interval i1 i2))
(define r2 (mul-interval i1 i2))
(equal-intervals? r1 r2))
(define i1 (make-interval -10 -5))
(define i2 (make-interval -10 5))
(define i3 (make-interval 5 10))
(same-mul? i1 i1)
(same-mul? i2 i1)
(same-mul? i3 i1)
(same-mul? i1 i2)
(same-mul? i2 i2)
(same-mul? i3 i2)
(same-mul? i1 i3)
(same-mul? i2 i3)
(same-mul? i3 i3)
Output
#t #t #t #t #t #t #t #t #t
SICP Exercise 2.10
Code
#lang racket
; v1
(provide div-interval)
(define make-interval cons)
(define lower-bound car)
(define upper-bound cdr)
(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)
(define y1 (lower-bound y))
(define y2 (upper-bound y))
(if (and (<= y1 0) (>= y2 0))
(error "division by interval with zero")
(mul-interval
x
(make-interval (/ 1.0 y2)
(/ 1.0 y1)))))
; tests
;(div-interval (make-interval 8 10) (make-interval 2 4)) ; '(2.0 . 5.0)
;(div-interval (make-interval 8 10) (make-interval -8 8)) ; division by interval with zero
SICP Exercise 2.8
Code
#lang racket ; v1 (require "ex2.7.scm") (define (sub-interval x y) (make-interval (- (lower-bound x) (upper-bound y)) (- (upper-bound x) (lower-bound y))))
SICP Exercise 2.7
Code
#lang racket
; v1
(provide make-interval)
(provide lower-bound)
(provide upper-bound)
(provide add-interval)
(provide mul-interval)
(define make-interval cons)
(define lower-bound car)
(define upper-bound cdr)
(define (add-interval x y)
(make-interval (+ (lower-bound x) (lower-bound y))
(+ (upper-bound x) (upper-bound y))))
(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))))
SICP Exercise 2.6
Code
#lang racket ; v1 (require "common.scm") (define zero (lambda (f) (lambda (x) x))) (define (add-1 n) (lambda (f) (lambda (x) (f ((n f) x))))) (define one (add-1 zero)) (define myone (lambda (f) (lambda (x) (f x)))) (define two (add-1 (add-1 zero))) (define mytwo (lambda (f) (lambda (x) (f (f x))))) (define (plus m n) (lambda (f) (lambda (x) ((m f) ((n f) x))))) ; tests ((one inc) 0) ((myone inc) 0) ((two inc) 0) ((mytwo inc) 0) (((plus one one) inc) 0) (((plus myone myone) inc) 0) (((plus two two) inc) 0) (((plus mytwo mytwo) inc) 0)
Output
1 1 2 2 2 2 4 4
SICP Exercise 2.5
Code
#lang racket
; v1
(define (cons x y)
(lambda (m) (m (* (expt 2 x) (expt 3 y)))))
(define (car z)
(z (lambda (r)
(define (iter r res)
(if (even? r)
(iter (quotient r 2) (+ res 1))
res))
(iter r 0))))
(define (cdr z)
(z (lambda (r)
(define (iter r res)
(if (and (> r 1) (= 0 (remainder r 3)))
(iter (quotient r 3) (+ res 1))
res))
(iter r 0))))
; tests
(define a (cons 10 20))
(car a)
(cdr a)
Output
10 20
SICP Exercise 2.4
Code
#lang racket ; v1 (define (cons x y) (lambda (m) (m x y))) (define (car z) (z (lambda (p q) p))) (define (cdr z) (z (lambda (p q) q))) ; tests (define a (cons 10 20)) (car a) (cdr a)
Output
10 20