-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path3_29.rkt
49 lines (41 loc) · 1.23 KB
/
3_29.rkt
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#lang sicp
#| Solution for exercise 3_29. |#
#| or gate on and-gate and three inverts. |#
(#%provide or-gate)
(define (inverter input output)
(define (invert-input)
(let ((new-value (logical-not (get-signal input))))
(after-delay inverter-delay
(lambda ()
(set-signal! output new-value)))))
(add-action! input invert-input)
'ok)
(define (logical-not s)
(cond ((= s 0) 1)
((= s 1) 0)
(else (error "Invalid signal" s))))
(define (and-gate a1 a2 output)
(define (and-action-procedure)
(let ((new-value
(logical-and (get-signal a1) (get-signal a2))))
(after-delay and-gate-delay
(lambda ()
(set-signal! output new-value)))))
(add-action! a1 and-action-procedure)
(add-action! a2 and-action-procedure)
'ok)
(define (logical-and s t)
(cond ((and (= s 1) (= t 1)) 1)
((and (= s 1) (= t 0)) 0)
((and (= s 0) (= t 1)) 0)
((and (= s 0) (= t 0)) 0)
(else (error "Invalid signal" s))))
(define (or-gate in1 in2 out)
(let ((a (make-wire))
(b (make-wire))
(c (make-wire)))
(inverter in1 a)
(inverter in2 b)
(and-gate a b c)
(inverter c out)
'ok))