44 lines
1.0 KiB
Racket
44 lines
1.0 KiB
Racket
#lang sicp
|
|
|
|
(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))))
|
|
|
|
;; *following uses logical-and -- see ch3support.scm
|
|
|
|
(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)
|
|
|
|
;; a or b
|
|
;; (not (not (a or b)))
|
|
;; (not ((not a) and (not b)))
|
|
|
|
(define (or-gate a1 a2 output)
|
|
(let ((c (make-wire))
|
|
(d (make-wire))
|
|
(e (make-wire)))
|
|
(inverter a c)
|
|
(inverter b d)
|
|
(and-gate c d e)
|
|
(inverter e output)
|
|
'ok))
|
|
|