关闭

练习 2.14 2.7 2.8 ~ 2.16 未完...

标签: sicp
165人阅读 评论(0) 收藏 举报
分类:
#lang racket


;: 区间加法
(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)
  (mul-interval x
                (make-interval (/ 1.0 (upper-bound y))
                               (/ 1.0 (lower-bound y)))))

;: 练习  2.7
(define (make-interval a b) (cons a b))

;: 答安
(define (upper-bound x)
  (let ((a (car x))
        (b (cdr x)))
    (if (> a b)
        a
        b)))

(define (lower-bound x)
  (let ((a (car x))
        (b (cdr x)))
    (if (> a b)
        b
        a)))
;: 测试
(define x (make-interval 3 5))
(upper-bound x)
(lower-bound x)

;:练习2.8 两个区间的差
;: 差的最小值 =-?
;: 差的最大值 =-?
(define (sub-interval x y)
  (make-interval (- (lower-bound x) (lower-bound y))
                 (- (upper-bound x) (upper-bound y))))

;: 练习 2.9 区间的宽带 =上界下界之差的一半
;: 宽度
(define (width-interval x)
  (/ 2 ( + (upper-bound x)
           (lower-bound x))))

;: 对于+法
;(define (add-interval x y)
;  (make-interval (+ (lower-bound x) (lower-bound y))
;                 (+ (upper-bound x) (upper-bound y))))

(define a (make-interval '1 '2))
(define b (make-interval '3 '4))

;(add-interval a b) =>
;  (make-interval (+ (lower-bound a) (lower-bound b))
;                 (+ (upper-bound a) (upper-bound b))))
;=>(make-interval (+ '1 '3) (+ '2 '4))
;=> 宽度= (/ 2
;          (- (+ '2 4)
;             (+ '1 '3)))
;: '2 - '1 + '4 - '3 / 2                   => 
;: (('2 - '1) + ('4 - '3)) / 2             =>
;: (/ 2 (+ (- '2 '1)
;:         (- '4 '3)))                     =>  
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:6942次
    • 积分:720
    • 等级:
    • 排名:千里之外
    • 原创:35篇
    • 转载:7篇
    • 译文:27篇
    • 评论:0条