sicp 习题 2.6 ~ 2.10

2.6
(define zero (lambda (f) (lambda (x) x)))

(define (add-1 n)
(lambda (f) (lambda (x) (f ((n f) x)))))

((zero 4) 3)
(add-1 5 1)

2.7
(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)))))

(define (make-interval a b) (cons a b))

(define (lower-bound x) (car x))

(define (upper-bound x) (cdr x))

(define interval-1 (make-interval 1.1 2.1))

(define interval-2 (make-interval 1.2 2.2))

(define new-interval (add-interval one-interval two-interval))

(lower-bound new-interval)
(upper-bound new-interval)

2.8
(define (add-interval x y)
(make-interval (+ (lower-bound x) (lower-bound y))
(+ (upper-bound x) (upper-bound y))))

(define (sub-interval x y)
(add-interval x (make-interval (- 0 (lower-bound y)) (- 0 (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)))))

(define (make-interval a b) (cons a b))

(define (lower-bound x) (car x))

(define (upper-bound x) (cdr x))

(define interval-1 (make-interval 1.1 2.1))

(define interval-2 (make-interval 1.2 2.2))

(define new-add-interval (add-interval interval-1 interval-2))

(define new-mul-interval (mul-interval interval-1 interval-2))

(define new-div-interval (div-interval interval-1 interval-2))

(define new-sub-interval (sub-interval interval-1 interval-2))

(lower-bound new-sub-interval)

(upper-bound new-sub-interval)

2.9
(define (add-interval x y)
(make-interval (+ (lower-bound x) (lower-bound y))
(+ (upper-bound x) (upper-bound y))))

(define (sub-interval x y)
(add-interval x (make-interval (- 0 (lower-bound y)) (- 0 (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)))))

(define (make-interval a b) (cons a b))

(define (lower-bound x) (car x))

(define (upper-bound x) (cdr x))

(define (interval-width interval)
(/ (- (upper-bound interval) (lower-bound interval)) 2))

(define (oper-interval-width v1 v2)
(let ((w1 (interval-width v1))
(w2 (interval-width v2)))
(lambda(x) (x w1 w2))))

(define interval-1 (make-interval 1.1 2.1))

(define interval-2 (make-interval 1.2 2.2))

(interval-width (add-interval interval-1 interval-2))

((oper-interval-width interval-1 interval-2) +)

(interval-width (sub-interval interval-1 interval-2))

((oper-interval-width interval-1 interval-2) -)

(interval-width (mul-interval interval-1 interval-2))

((oper-interval-width interval-1 interval-2) *)

(interval-width (div-interval interval-1 interval-2))

((oper-interval-width interval-1 interval-2) /)

2.10
(define (add-interval x y)
(make-interval (+ (lower-bound x) (lower-bound y))
(+ (upper-bound x) (upper-bound y))))

(define (sub-interval x y)
(add-interval x (make-interval (- 0 (lower-bound y)) (- 0 (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 ((product-y (* (lower-bound y) (upper-bound y))))
(if (< product-y 0)
(display "illegal number")
(mul-interval x
(make-interval (/ 1.0 (upper-bound y)) (/ 1.0 (lower-bound y)))))))

(define (make-interval a b) (cons a b))

(define (lower-bound x) (car x))

(define (upper-bound x) (cdr x))

(define (interval-width interval)
(/ (- (upper-bound interval) (lower-bound interval)) 2))

(define (oper-interval-width v1 v2)
(let ((w1 (interval-width v1))
(w2 (interval-width v2)))
(lambda(x) (x w1 w2))))

(define interval-1 (make-interval 1.1 2.1))

(define interval-2 (make-interval -1.2 2.2))

(div-interval interval-1 interval-2)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值