免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12345下一页
最近访问板块 发新帖
查看: 14471 | 回复: 41
打印 上一主题 下一主题

SICP 习题参考答案 2.3  关闭 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-10-05 10:37 |只看该作者 |倒序浏览
此帖为 SICP 章节 2.3 的参考答案。为保持可读性,请勿直接在本帖讨论。讨论请另开新帖。


论坛徽章:
0
2 [报告]
发表于 2008-10-05 10:38 |只看该作者

Exercise 2.53.

Exercise 2.53.  What would the interpreter print in response to evaluating each of the following expressions?

(list 'a 'b 'c)

(list (list 'george))
(cdr '((x1 x2) (y1 y2)))

(cadr '((x1 x2) (y1 y2)))
(pair? (car '(a short list)))
(memq 'red '((red shoes) (blue socks)))

(memq 'red '(red shoes blue socks))

论坛徽章:
0
3 [报告]
发表于 2008-10-05 10:44 |只看该作者

Exercise 2.53. 答案


  1. > (list 'a 'b 'c)
  2. (a b c)

  3. > (list (list 'george))
  4. ((george))

  5. > (cdr '((x1 x2) (y1 y2)))
  6. ((y1 y2))

  7. > (cadr '((x1 x2) (y1 y2)))
  8. (y1 y2)

  9. > (pair? (car '(a short list)))
  10. #f

  11. > (memq 'red '((red shoes) (blue socks)))
  12. #f

  13. > (memq 'red '(red shoes blue socks))
  14. (red shoes blue socks)
复制代码

论坛徽章:
0
4 [报告]
发表于 2008-10-05 10:45 |只看该作者

Exercise 2.54.

Exercise 2.54.  Two lists are said to be equal? if they contain equal elements arranged in the same order. For example,

(equal? '(this is a list) '(this is a list))

is true, but

(equal? '(this is a list) '(this (is a) list))

is false. To be more precise, we can define equal? recursively in terms of the basic eq? equality of symbols by saying that a and b are equal? if they are both symbols and the symbols are eq?, or if they are both lists such that (car a) is equal? to (car b) and (cdr a) is equal? to (cdr b). Using this idea, implement equal? as a procedure.

论坛徽章:
0
5 [报告]
发表于 2008-10-05 11:01 |只看该作者

Exercise 2.54. 答案


  1. (define (Equal? a b)
  2.   (if (and (pair? a) (pair? b))
  3.       (and (Equal? (car a) (car b))
  4.            (Equal? (cdr a) (cdr b)))
  5.       (eq? a b)))
复制代码

[ 本帖最后由 win_hate 于 2008-10-5 11:03 编辑 ]

论坛徽章:
0
6 [报告]
发表于 2008-10-05 11:03 |只看该作者

Exercise 2.55.

Exercise 2.55.  Eva Lu Ator types to the interpreter the expression

(car ''abracadabra)

To her surprise, the interpreter prints back quote. Explain.

[ 本帖最后由 win_hate 于 2008-10-5 11:08 编辑 ]

论坛徽章:
0
7 [报告]
发表于 2008-10-05 11:05 |只看该作者

Exercise 2.55. 答案

这是因为 'x 是个缩写,等价于 (quote x)。所以 ''abracadabra 是 (quote (quote abracadabra)).

论坛徽章:
0
8 [报告]
发表于 2008-10-22 10:23 |只看该作者

Exercise 2.56.

Exercise 2.56.  Show how to extend the basic differentiator to handle more kinds of expressions. For instance, implement the differentiation rule



by adding a new clause to the deriv program and defining appropriate procedures exponentiation?, base, exponent, and make-exponentiation. (You may use the symbol ** to denote exponentiation.) Build in the rules that anything raised to the power 0 is 1 and anything raised to the power 1 is the thing itself.

论坛徽章:
0
9 [报告]
发表于 2008-10-22 10:29 |只看该作者

Exercis2 2.56. 答案


  1. (define (exponentiation? exp)
  2.   (and (pair? exp) (eq? (car exp) '**)))

  3. (define base cadr)
  4. (define exponent caddr)

  5. (define (make-exponentiation b e)
  6.     (cond ((=number? e 0) 1)
  7.         ((=number? b 1) 1)
  8.         ((and (number? b) (number? e)) (expt b e))
  9.         (else (list '** b e))))
复制代码


  1. (define (deriv exp var)
  2.   (cond ((number? exp) 0)
  3.         ((variable? exp)
  4.          (if (same-variable? exp var) 1 0))
  5.         ((sum? exp)
  6.          (make-sum (deriv (addend exp) var)
  7.                    (deriv (augend exp) var)))
  8.         ((product? exp)
  9.          (make-sum
  10.           (make-product (multiplier exp)
  11.                         (deriv (multiplicand exp) var))
  12.           (make-product (deriv (multiplier exp) var)
  13.                         (multiplicand exp))))
  14.         ((exponentiation? exp)
  15.          (make-product
  16.           (make-product
  17.            (exponent exp)
  18.            (make-exponentiation (base exp)
  19.                                 (- (exponent exp) 1)))
  20.           (deriv (base exp) var)))
  21.          (else
  22.           (error "unknown expression type -- DERIV" exp))))
复制代码


  1. > (deriv '(** x 1) 'x)
  2. 1
  3. > (deriv '(** x 10) 'x)
  4. (* 10 (** x 9))
  5. > (deriv (make-exponentiation 'x 0) 'x)
  6. 0
复制代码

论坛徽章:
0
10 [报告]
发表于 2008-10-28 17:49 |只看该作者

Exercise 2.57.

Exercise 2.57.  Extend the differentiation program to handle sums and products of arbitrary numbers of (two or more) terms. Then the last example above could be expressed as

(deriv '(* x y (+ x 3)) 'x)

Try to do this by changing only the representation for sums and products, without changing the deriv procedure at all. For example, the addend of a sum would be the first term, and the augend would be the sum of the rest of the terms.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP