灯下 登录
计算机科学 / SICP / 3.3.5 Propagation of Constraints

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.

练习 3.37:与更具表达式风格的定义相比,celsius-fahrenheit-converter 过程显得较为笨拙,例如:

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

这里 c+、c* 等是算术运算的“约束”版本。例如,c+ 以两个连接器为参数,返回一个通过加法器约束与这两者相关联的连接器:

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

请定义类似的过程 c-、c*、c/ 和 cv(常量值),使我们能像上面的转换器示例那样定义复合约束。

Racket #lang sicp
(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))
Racket #lang sicp
(define (c+ x y)
 (let ((z (make-connector)))
 (adder x y z)
 z))