3.28 Exercise 3.28
Implementing or-gate by copying and-gate is fairly trivial:
| (define or-gate-delay 5) |
| (define (or-gate o1 o2 output) | | (define (or-action-procedure) | | (let ((new-value (logical-or (get-signal o1) (get-signal o2)))) | | (after-delay or-gate-delay | | (lambda () (set-signal! output new-value))))) | | (add-action! o1 or-action-procedure) | | (add-action! o2 or-action-procedure) | | 'ok) |
|
| (define (logical-or a b) | | (cond ((and (= a 0) (= b 0)) 0) | | ((and (= a 1) (= b 0)) 1) | | ((and (= a 0) (= b 1)) 1) | | ((and (= a 1) (= b 1)) 1) | | (else (error "Invalid signals" a b)))) |
|
The fact that this is so similar to and-gate is a clue
that a more general function is hiding – in this case, that
of the binary operation:
| (define (binary-op op delay) | | (lambda (x y output) | | (define (binary-action-procedure) | | (let ((new-value (op (get-signal x) (get-signal y)))) | | (after-delay delay | | (lambda () (set-signal! output new-value))))) | | (add-action! x binary-action-procedure) | | (add-action! y binary-action-procedure) | | 'ok)) |
|
| (define and-gate (binary-op logical-and and-gate-delay)) |
| (define or-gate (binary-op logical-or or-gate-delay)) |