免费注册 查看新帖 |

Chinaunix

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

SICP 习题参考答案 3.3 [复制链接]

论坛徽章:
0
51 [报告]
发表于 2009-01-03 10:26 |只看该作者

Exercise 3.35.

Exercise 3.35.  Ben Bitdiddle tells Louis that one way to avoid the trouble in exercise 3.34 is to define a squarer as a new primitive constraint. Fill in the missing portions in Ben's outline for a procedure to implement such a constraint:

(define (squarer a b)
  (define (process-new-value)
    (if (has-value? b)
        (if (< (get-value b) 0)
            (error "square less than 0 -- SQUARER" (get-value b))
            <alternative1>)
        <alternative2>))
  (define (process-forget-value) <body1>)
  (define (me request) <body2>)
  <rest of definition>
  me)

论坛徽章:
0
52 [报告]
发表于 2009-01-03 10:30 |只看该作者

Exercise 3.35. 答案


  1. (define (squarer a b)
  2.   (define (process-new-value)
  3.     (if (has-value? b)
  4.         (if (< (get-value b) 0)
  5.             (error "square less than 0 -- SQUARER" (get-value b))
  6.             (set-value! a (sqrt (get-value b)) me))
  7.         (if (has-value? a)
  8.             (set-value! b (* (get-value a) (get-value a)) me))))
  9.     (define (process-forget-value)
  10.       (forget-value! a me)
  11.       (forget-value! b me)
  12.       (process-new-value))
  13.     (define (me request)
  14.       (cond ((eq? request 'I-have-a-value)
  15.              (process-new-value))
  16.             ((eq? request 'I-lost-my-value)
  17.              (process-forget-value))
  18.             (else
  19.              (error "Unknown request -- SQUARER" request))))
  20.     (connect a me)
  21.     (connect b me)
  22.     me)
复制代码


演示使用的代码


  1. (load "3.35.scm")

  2. (define a (make-connector))
  3. (define b (make-connector))


  4. (squarer a b)
  5. (probe 'a a)
  6. (probe 'b b)
复制代码


交互环境中的演示:


  1. > (load "test.scm")
  2. > (set-value! a 100 'zxl)

  3. Probe: a = 100
  4. Probe: b = 10000done
  5. > (forget-value! a 'zxl)

  6. Probe: a = ?
  7. Probe: b = ?done
  8. > (set-value! b 100 'zxl)

  9. Probe: b = 100
  10. Probe: a = 10.0done
  11. > (forget-value! b 'zxl)

  12. Probe: b = ?
  13. Probe: a = ?done
  14. > (set-value! a -10 'zxl)

  15. Probe: a = -10
  16. Probe: b = 100done
复制代码

论坛徽章:
0
53 [报告]
发表于 2009-01-04 11:43 |只看该作者

Exercise 3.36.

Exercise 3.36.  Suppose we evaluate the following sequence of expressions in the global environment:

(define a (make-connector))
(define b (make-connector))
(set-value! a 10 'user)

At some time during evaluation of the set-value!, the following expression from the connector's local procedure is evaluated:

(for-each-except setter inform-about-value constraints)

Draw an environment diagram showing the environment in which the above expression is evaluated.

论坛徽章:
0
54 [报告]
发表于 2009-01-04 11:43 |只看该作者

Exercise 3.36. 答案

<wait for pic>

论坛徽章:
0
55 [报告]
发表于 2009-01-04 11:44 |只看该作者

Exercise 3.37.

Exercise 3.37.  The celsius-fahrenheit-converter procedure is cumbersome when compared with a more expression-oriented style of definition, such as

(define (celsius-fahrenheit-converter x)
  (c+ (c* (c/ (cv 9) (cv 5))
          x)
      (cv 32)))
(define C (make-connector))
(define F (celsius-fahrenheit-converter C))

Here c+, c*, etc. are the ``constraint'' versions of the arithmetic operations. For example, c+ takes two connectors as arguments and returns a connector that is related to these by an adder constraint:

(define (c+ x y)
  (let ((z (make-connector)))
    (adder x y z)
    z))

Define analogous procedures c-, c*, c/, and cv (constant value) that enable us to define compound constraints as in the converter example above.

论坛徽章:
0
56 [报告]
发表于 2009-01-04 11:44 |只看该作者

Exercise 3.37. 答案


  1. (define (c+ x y)
  2.   (let ((z (make-connector)))
  3.     (adder x y z)
  4.     z))

  5. (define (c* x y)
  6.   (let ((z (make-connector)))
  7.     (multiplier x y z)
  8.     z))

  9. (define (c/ x y)
  10.   (let ((z (make-connector)))
  11.     (multiplier z y x)
  12.     z))

  13. (define (cv c)
  14.   (let ((x (make-connector)))
  15.     (constant c x)
  16.     x))

  17. (define (celsius-fahrenheit-converter x)
  18.   (c+ (c* (c/ (cv 9) (cv 5))
  19.           x)
  20.       (cv 32)))

  21. (define C (make-connector))
  22. (define F (celsius-fahrenheit-converter C))
  23. (probe 'C C)
  24. (probe 'F F)
复制代码

论坛徽章:
0
57 [报告]
发表于 2009-01-05 11:47 |只看该作者
本节完。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP