Skip to content

Commit 4787c98

Browse files
committed
Add implementation of exercise 3-33
1 parent 057aef1 commit 4787c98

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

README.org

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
习题完成情况:
2626
- 章节一: 43/46
2727
- 章节二: 88/97
28-
- 章节三: 32/82
28+
- 章节三: 33/82
2929
- 章节四: TODO
3030
- 章节五: TODO
3131
* 运行

chapter3/exercise3-33.rkt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#lang racket
2+
(require "constraints.rkt")
3+
;;; c = (a + b) * 0.5
4+
5+
(define (averager a b c)
6+
(let ((sum (make-connector))
7+
(half (make-connector))
8+
)
9+
(constant 0.5 half)
10+
(adder a b sum)
11+
(multiplier sum half c)
12+
'ok))
13+
14+
(module+ test
15+
(require rackunit)
16+
17+
(test-case "Test for averager with a and b are set"
18+
(define a (make-connector))
19+
(define b (make-connector))
20+
(define c (make-connector))
21+
(averager a b c)
22+
(probe "value a " a)
23+
(probe "value b " b)
24+
(probe "value c " c)
25+
(check-false (has-value? c))
26+
(set-value! a 2 'user)
27+
(set-value! b 4 'user)
28+
(check-equal? (get-value c) 3.0)
29+
(check-exn (regexp "Contradiction") (lambda () (set-value! a 212 'user)))
30+
)
31+
32+
(test-case "Test for averager with c and b are set"
33+
(define a (make-connector))
34+
(define b (make-connector))
35+
(define c (make-connector))
36+
(averager a b c)
37+
(probe "value a " a)
38+
(probe "value b " b)
39+
(probe "value c " c)
40+
(check-false (has-value? c))
41+
(set-value! b 4 'user)
42+
(set-value! c 0 'user)
43+
(check-true (has-value? a))
44+
(check-equal? (get-value a) -4)
45+
)
46+
)

0 commit comments

Comments
 (0)