乐正

Actions speak louder than words.

Sicp-ex3-28

问题

请将或门定义为一个基本功能模块。你的or-gate构造函数应该和上面的and-gate构造函数类似。

解答

练习3.28 (ex3-28.scm) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
(define (or-gate input-1 input-2 output)
  (define (or-action-procedure)
    (let ((new-value (logical-or (get-signal input-1)
                                 (get-signal input-2))))
      (after-delay or-gate-delay
                   (lambda ()
                     (set-signal! output new-value)))))
  (add-action! input-1 or-action-procedure)
  (add-action! input-2 or-action-procedure)
  'ok)

(define (logical-or s1 s2)
  (cond ((or (= s1 1) (= s2 1)) 1)
        ((and (= s1 0) (= s2 0)) 0)
        (else (error "Invalid signal" s1 s2))))

draft

« sicp-ex3-27 sicp-ex3-29 »

Comments