乐正

Actions speak louder than words.

Sicp-ex3-33

问题

利用基本的加法、乘法和常量约束定义一个average过程,它以三个连接abc作为输入,建立起一个约束,使得c总是ab的平均值。

解答

练习3.33 (ex3-33.scm) download
1
2
3
4
5
6
7
(define (average a b c)
  (let ((coef (make-connector))
        (temp (make-connector)))
    (adder a b temp)
    (constant 2 coef)
    (multiplier coef c temp)
    'ok))

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
(define a (make-connector)
(define b (make-connector))
(define c (make-connector))

(average a b c)
$24 = ok

(probe "A" a)
$25 = #<procedure me (request)>

(probe "B" b)
$26 = #<procedure me (request)>

(probe "C" c)
$27 = #<procedure me (request)>

(set-value! a 10 'user)
Probe: A = 10
$28 = done

(set-value! b 20 'user)

Probe: B = 20
Probe: C = 15
$29 = done

draft

« sicp-ex3-32 sicp-ex3-34 »

Comments