46 lines
1.0 KiB
Racket
46 lines
1.0 KiB
Racket
#lang sicp
|
|
|
|
;;For Section 3.3.4, used by and-gate
|
|
(define (logical-and x y)
|
|
(cond ((and (= x 1) (= y 1)) 1)
|
|
((or
|
|
(and (= x 0) (= y 1))
|
|
(and (= x 1) (= y 0)))
|
|
0)
|
|
(else
|
|
(error "Invalid signal"))))
|
|
|
|
(define (logical-or x y)
|
|
(cond ((and (= x 0) (= y 0)) 0)
|
|
((or
|
|
(and (= x 0) (= y 1))
|
|
(and (= x 1) (= y 0))
|
|
(and (= x 1) (= y 1)))
|
|
1)
|
|
(else
|
|
(error "Invalid signals" x y))))
|
|
|
|
(define (or-gate a1 a2 output)
|
|
(define (or-action-procedure)
|
|
(let ((new-value
|
|
(logical-or (get-signal a1) (get-signal a2))))
|
|
(after-delay or-gate-delay
|
|
(lambda ()
|
|
(set-signal! output new-value)))))
|
|
(add-action! a1 or-action-procedure)
|
|
(add-action! a2 or-action-procedure)
|
|
'ok)
|
|
|
|
|
|
(#%require (only racket/base module+))
|
|
|
|
(module+ test
|
|
(#%require rackunit)
|
|
|
|
(test-begin
|
|
(check-equal? (logical-or 0 0) 0)
|
|
(check-equal? (logical-or 1 0) 1)
|
|
(check-equal? (logical-or 0 1) 1)
|
|
(check-equal? (logical-or 1 1) 1)))
|
|
|