SICP Exercise 3.37

SICP Exercise 3.37

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

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

(define (c* x y)
  (let ((z (make-connector)))
    (multiplier x y z)
    z))

(define (c/ x y)
  (let ((z (make-connector)))
    (multiplier z y x)
    z))

(define (cv x)
  (let ((z (make-connector)))
    (constant x z)
    z))

(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))
(probe "Celsius temp" C)
(probe "Fahrenheit temp" F)

;;;Test
(set-value! C 25 'user)
(set-value! C 26 'user)
(forget-value! C 'user)
(set-value! F 24 'user)
The output is:

#<procedure:me>
#<procedure:me>
Probe: Celsius temp = 25
Probe: Fahrenheit temp = 77
'done
Error:connector.rkt:15:13: Contradiction (25 26)
when encounter the error, the execution is stopped. If we change the test code to:

;;;Test
(set-value! C 25 'user)
;(set-value! C 26 'user)
(forget-value! C 'user)
(set-value! F 24 'user)

The output is:

#<procedure:me>
#<procedure:me>
Probe: Celsius temp = 25
Probe: Fahrenheit temp = 77
'done
Probe: Celsius temp = ?
Probe: Fahrenheit temp = ?
'done
Probe: Fahrenheit temp = 24
Probe: Celsius temp = -40/9
'done




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值