免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: win_hate
打印 上一主题 下一主题

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

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

Exercise 2.57. 答案

只要把两个选择函数略为修改就可以了:


  1. (define (augend s)
  2.   (let ((rest (cddr s)))
  3.     (if (null? (cdr rest))
  4.         (car rest)
  5.         (cons '+ rest))))

  6. (define (multiplicand p)
  7.   (let ((rest (cddr p)))
  8.     (if (null? (cdr rest))
  9.         (car rest)
  10.         (cons '* rest))))
复制代码


  1. > (deriv '(* x y (+ x 3)) 'x)
  2. (+ (* x y) (* y (+ x 3)))
复制代码


前缀表达式不是很好看。对比一下 maxima 的微分


  1. (%i1) diff(x*y*(x+3), x);
  2. (%o1)                           (x + 3) y + x y
复制代码

[ 本帖最后由 win_hate 于 2008-10-29 10:15 编辑 ]

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

Exercise 2.58.

Exercise 2.58.  Suppose we want to modify the differentiation program so that it works with ordinary mathematical notation, in which + and * are infix rather than prefix operators. Since the differentiation program is defined in terms of abstract data, we can modify it to work with different representations of expressions solely by changing the predicates, selectors, and constructors that define the representation of the algebraic expressions on which the differentiator is to operate.

a. Show how to do this in order to differentiate algebraic expressions presented in infix form, such as (x + (3 * (x + (y + 2)))). To simplify the task, assume that + and * always take two arguments and that expressions are fully parenthesized.

b. The problem becomes substantially harder if we allow standard algebraic notation, such as (x + 3 * (x + y + 2)), which drops unnecessary parentheses and assumes that multiplication is done before addition. Can you design appropriate predicates, selectors, and constructors for this notation such that our derivative program still works?

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

Exercise 2.58. 答案

把 a, b 一起做了。

  • 这个题目并没有看上去那么难。因为只有两种运算,而且 scheme 天生具有处理括号的能力,所以不用考虑括号平衡的问题。
  • 为了去除多余的括号,比如 (x), ((x+y)),加进了一个 strip 函数
  • 为了 make-product 和 make-sum 不产生多余的括号,相应的代码有点胖
  • 满足了题目 “只修改建构函数和选择函数,不改动 deriv" 的要求。



  1. (define (strip exp)
  2.   (if (and (list? exp) (null? (cdr exp)))
  3.       (strip (car exp))
  4.       exp))

  5. (define (sum? s)
  6.   (member '+ s))

  7. (define (addend s)
  8.   (define (f ini xs)
  9.     (let ((x (car xs)))
  10.       (if (eq? x '+)
  11.           ini
  12.           (f (append ini (list x)) (cdr xs)))))
  13.   (strip (f '() s)))
  14.   
  15. (define (augend s)
  16.   (strip (cdr (member '+ s))))

  17. (define (product? s)
  18.   (and (member '* s) (not (member '+ s))))

  19. (define (multiplier p)
  20.   (strip (car p)))

  21. (define (multiplicand p)
  22.   (strip (cddr p)))

  23. (define (make-sum a1 a2)
  24.   (cond ((=number? a1 0) (strip a2))
  25.         ((=number? a2 0) (strip a1))
  26.         ((and (number? a1) (number? a2)) (+ a1 a2))
  27.         (else (let ((A1 (if (list? a1) a1 (list a1)))
  28.                     (A2 (if (list? a2) a2 (list a2))))
  29.                 (append A1 (cons '+ A2))))))

  30. (define (make-product m1 m2)
  31.   (cond ((or (=number? m1 0) (=number? m2 0)) 0)
  32.         ((=number? m1 1) (strip m2))
  33.         ((=number? m2 1) (strip m1))
  34.         ((and (number? m1) (number? m2)) (* m1 m2))
  35.         (else (let ((M1 (if (list? m1)
  36.                             (if (sum? m1) (list m1) m1)
  37.                             (list m1)))
  38.                     (M2 (if (list? m2)
  39.                             (if (sum? m2) (list m2) m2)
  40.                             (list m2))))
  41.                 (append M1 (cons '* M2))))))
复制代码


演示:


  1. > (deriv '(x + 3) 'x)
  2. 1

  3. > (deriv '(x * y) 'x)
  4. y

  5. > (deriv '( x * y * ( x + 3)) 'x)
  6. (x * y + y * (x + 3))

  7. > (deriv '(x + (3 * (x + (y + 2)))) 'x)
  8. 4

  9. > (deriv '(x + 3 * (x + y + 2)) 'x)
  10. 4
复制代码

[ 本帖最后由 win_hate 于 2008-10-29 09:52 编辑 ]

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

Exercise 2.59.

Exercise 2.59.  Implement the union-set operation for the unordered-list representation of sets.

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

Exercise 2.59. 答案


  1. (define (union-set set1 set2)
  2.   (if (null? set1) set2
  3.       (union-set (cdr set1)
  4.                 (adjoin-set (car set1) set2))))
复制代码

[ 本帖最后由 win_hate 于 2008-10-30 23:19 编辑 ]

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

Exercise 2.60.

Exercise 2.60.  We specified that a set would be represented as a list with no duplicates. Now suppose we allow duplicates. For instance, the set {1,2,3} could be represented as the list (2 3 2 1 3 2 2). Design procedures element-of-set?, adjoin-set, union-set, and intersection-set that operate on this representation. How does the efficiency of each compare with the corresponding procedure for the non-duplicate representation? Are there applications for which you would use this representation in preference to the non-duplicate one?

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

  1. (define (adjoin-set x set)
  2.   (cons x set))

  3. (define (union-set set1 set2)
  4.   (append set1 set2))
复制代码


其它函数不变。adjoin 变成 O(1) 的了。 union-set 的效率与 set1 的长度成线性关系。作用在相同集合上时,其它函数效率不变。但由于构造出来的数据含有重复的元素,会引会引发数据膨胀。

[ 本帖最后由 win_hate 于 2008-10-30 23:29 编辑 ]

论坛徽章:
0
18 [报告]
发表于 2008-10-31 09:42 |只看该作者

Exercise 2.61.

Exercise 2.61.  Give an implementation of adjoin-set using the ordered representation. By analogy with element-of-set? show how to take advantage of the ordering to produce a procedure that requires on the average about half as many steps as with the unordered representation.

论坛徽章:
0
19 [报告]
发表于 2008-10-31 09:48 |只看该作者

  1. (define (adjoin-set x set)
  2.   (if (null? set) (cons x '())
  3.       (let ((y (car set)))
  4.         (cond ((= x y) set)
  5.               ((> x y) (cons y (adjoin-set x (cdr set))))
  6.               (else (cons x set))))))
复制代码


书本上 “requires on the average about half as many steps” 疑有误。已排序的情况下,查找的次数是少了,但对插入元素的位置有要求。构造新表的开销比无需排序时大。

论坛徽章:
0
20 [报告]
发表于 2008-10-31 09:50 |只看该作者

Exercise 2.62.

Exercise 2.62.  Give a (n) implementation of union-set for sets represented as ordered lists.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP