SICP 习题2.11 改写div-interval 分情况讨论

xy
3*3总共有9种情况,分类讨论即可。
div-interval的代码
(define (div-interval x y)
  (let ((x1 (lower-bound x))
    (y1 (upper-bound x))
    (x2 (/ 1 (lower-bound y)))
    (y2 (/ 1 (upper-bound y))))
    (cond ((and (> x2 0) (> y2 0)) 
       (cond ((and (> x1 0) (> y1 0)) (make-interval (* x1 y2) (* y1 x2))) ((and (> x1 0) (< y1 0)) (make-interval (* y1 x2) (* x1 x2))) ((and (< x1 0) (< y1 0)) (make-interval (* x1 x2) (* y1 y2)))))
      ((and (< x2 0) (< y2 0))
       (cond ((and (> x1 0) (> y1 0)) (make-interval (* x1 x2) (* y1 y2))) ((and (> x1 0) (< y1 0)) (make-interval (* x1 y2) (* y1 y2))) ((and (< x1 0) (< y1 0)) (make-interval (* y1 x2) (* x1 y2)))))
      (else out-error))))
整个程序的代码
(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))))

(define (div-interval x y)
  (let ((x1 (lower-bound x))
    (y1 (upper-bound x))
    (x2 (/ 1 (lower-bound y)))
    (y2 (/ 1 (upper-bound y))))
    (cond ((and (> x2 0) (> y2 0)) 
       (cond ((and (> x1 0) (> y1 0)) (make-interval (* x1 y2) (* y1 x2))) ((and (> x1 0) (< y1 0)) (make-interval (* y1 x2) (* x1 x2))) ((and (< x1 0) (< y1 0)) (make-interval (* x1 x2) (* y1 y2)))))
      ((and (< x2 0) (< y2 0))
       (cond ((and (> x1 0) (> y1 0)) (make-interval (* x1 x2) (* y1 y2))) ((and (> x1 0) (< y1 0)) (make-interval (* x1 y2) (* y1 y2))) ((and (< x1 0) (< y1 0)) (make-interval (* y1 x2) (* x1 y2)))))
      (else out-error))))

(define (out-error)
  (newline)
  (display "ERROR"))

(define (sub-interval x y)
  (make-interval (- (lower-bound x) (upper-bound y))
         (- (upper-bound x) (lower-bound y))))

(define (width-interval z)
  (/ (- (upper-bound z) (lower-bound z)) 2.0))

(define make-interval cons)
(define lower-bound car)
(define upper-bound cdr)

(define print-interval
  (lambda (z)
    (newline)
    (display (lower-bound z))
    (display " ~ ")
    (display (upper-bound z))))

(define a (make-interval 1 2))
(define b (make-interval 3 4))
(newline)
(display "a : ")
(display (width-interval a))
(newline)
(display "b : ")
(display (width-interval b))

(print-interval (add-interval a b))
(newline)
(display "add : ")
(display (width-interval (add-interval a b)))

(print-interval (mul-interval a b))
(newline)
(display "mul : ")
(display (width-interval (mul-interval a b)))

(print-interval (div-interval a b))
(newline)
(display "div : ")
(display (width-interval (div-interval a b)))

(print-interval (sub-interval a b))
(newline)
(display "sub : ")
(display (width-interval (sub-interval a b)))

(define c (make-interval -1 2))
(div-interval a c)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值