练习2.2-练习2.10

15 篇文章 0 订阅

1.练习2.2

(define (make-segment start end)
  (cons start end))

(define (start-segment seg)
  (car seg))

(define (end-segment seg)
  (cdr seg))

(define (make-point x y)
  (cons x y))

(define (x-point p)
  (car p))

(define (y-point p)
  (cdr p))

(define (midpoint-segment seg)
  (make-point (/ (+ (x-point (start-segment seg))
		    (x-point (end-segment seg))) 2)
	      (/ (+ (y-point (start-segment seg))
		    (y-point (end-segment seg))) 2)))
2.练习2.3

首次定义:

(define (make-rect start end)
  (cons start end))

(define (start-rect seg)
  (car seg))

(define (end-rect seg)
  (cdr seg))
(define (width rect)
  (abs (- (y-point (end-rect rect))
	  (y-point (start-rect rect)))))

(define (length rect)
  (abs (- (x-point (end-rect rect))
	  (x-point (start-rect rect)))))

(define (girth rect)
  (* (+ (width rect)
	(length rect))
     2))

(define (area rect)
  (* (width rect)
     (length rect)))
二次定义:

(define (make-rect start length width)
  (cons start
	(cons length width)))

(define (start-rect seg)
  (car seg))

(define (end-rect seg)
  (cons (+ (x-point (start-rect seg))
	   (car (cdr seg)))
	(+ (y-point (start-rect seg))
	   (cdr (cdr seg)))))

3.练习2.4

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

4.练习2.5

(define (cons a b)
  (* (pow 2 a)
     (pow 3 b)))

(define (car c)
  (define (iter n result)
    (if (not (= (remainder n 2) 0))
	result
	(iter (/ n 2) (+ result 1))))
  (iter c 0))

(define (cdr c)
  (define (iter n result)
    (if (not (= (remainder n 3) 0))
	result
	(iter (/ n 3) (+ result 1))))
  (iter c 0))

5.练习2.6

(define one
  (lambda (f) (lambda (x) (f x))))

(define two
  (lambda (f) (lambda (x) (f (f x)))))
(define (add a b)
  (lambda (f) (lambda (x) ((b f) ((a f) x)))))
6.练习2.7

(define (make-interval x y)
  (cons x y))

(define (upper-bound c)
  (cdr c))

(define (lower-bound c)
  (car c))
7.练习2.8

(define (sub-interval x y)
  (let ((a (- (lower-bound x) (lower-bound y)))
	(b (- (upper-bound x) (upper-bound y))))
    (make-interval (min a b)
		   (max a b))))
8.练习2.9

(define (width-interval a)
  (/ (- (upper-bound a)
	(lower-bound a))
     2))
9.练习2.10

(define (div-interval x y)
  (if (and (<= (lower-bound y) 0) (>= (upper-bound y) 0))
      (error "ERROR!!!")
      (mul-interval x
		       (make-interval (/ 1.0 (upper-bound y))
				      (/ 1.0 (lower-bound y))))))

  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论

打赏作者

_海阔天空

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值