問題3.30 – SICP(計算機プログラムの構造と解釈)その128

問題3.30

繰上り伝播加算器

(define (ripple-carry-adder list-a list-b list-sum c-out)
  (define (iter list-a list-b list-sum c-in)
    (if (not (null? list-a))
        (let ((c-out (make-wire)))
             (full-adder (car list-a) (car list-b) c-in (car list-sum) c-out)
             (iter (cdr list-a) (cdr list-b) (cdr list-sum) c-out))
        'ok))
  (iter list-a list-b list-sum c-out))

実行結果

(define the-agenda (make-agenda))
(define inverter-delay 2)
(define and-gate-delay 3)
(define or-gate-delay 5)

(define a1 (make-wire))
(define a2 (make-wire))
(define a3 (make-wire))
(define a4 (make-wire))

(define b1 (make-wire))
(define b2 (make-wire))
(define b3 (make-wire))
(define b4 (make-wire))

(define s1 (make-wire))
(define s2 (make-wire))
(define s3 (make-wire))
(define s4 (make-wire))

(define a (list a1 a2 a3 a4))
(define b (list b1 b2 b3 b4))
(define s (list s1 s2 s3 s4))
(define c (make-wire))

(probe 's1 s1)
(probe 's2 s2)
(probe 's3 s3)
(probe 's4 s4)
(probe 'c c)

(ripple-carry-adder a b s c)
(set-signal! a1 1)
(propagate)
gosh> 
s1 8 New-value = 1done

(set-signal! b1 1)
(propagate)
gosh> 
s1 24 New-value = 0
s2 40 New-value = 1done

(set-signal! a2 1)
(propagate)
gosh> 
s2 48 New-value = 0
s3 64 New-value = 1done
a4 a3 a2 a1 + b4 b3 b2 b1 = s4 s3 s2 s1
 0  0  1  1 +  0  0  0  1 =  0  1  0  0

; ビット演算
0011 + 0010 = 0100
; 整数演算
   3 +    1 =    4
計算機プログラムの構造と解釈
ジェラルド・ジェイ サスマン ジュリー サスマン ハロルド エイブルソン
ピアソンエデュケーション
売り上げランキング: 6542
«
»