sicp-ex-3.30



<< Previous exercise (3.29) | Index | Next exercise (3.31) >>


genovia

  
 ;;a 
 (define (ripple-carry-adder Ak Bk Sk C) 
   (define (iter A B S c-in c-out) 
     (if (null? A) 
         S 
         (begin (full-adder (car A) (car B) 
                            c-in (car S) c-out) 
                (iter (cdr A) (cdr B) (cdr S) 
                      (c-out) (make-wire))))) 
   (iter Ak Bk Sk C (make-wire))) 
  
  
  
 ;;a 
 (define (ripple-carry-adder a b s c) 
   (let ((c-in (make-wire))) 
         (if (null? (cdr a)) 
           (set-signal! c-in 0) 
           (ripple-carry-adder (cdr a) (cdr b) (cdr s) c-in)) 
         (full-adder (car a) (car b) c-in (car s) c))) 
  
  
 (define (ripple-carry-adder a b c-in sum) 
     (if (not (null? a)) 
         (let ((carry (make-wire))) 
               (full-adder         (car a) (car b) c-in (car sum) carry) 
               (ripple-carry-adder (cdr a) (cdr b) carry (cdr sum))))) 
  
  
 ;;some convinience methods to test the adder 
 (define (build-wires input-signals) 
         (if (null? input-signals) 
             '() 
             (let ((new-wire (make-wire))) 
                  (set-signal! new-wire (car input-signals)) 
                  (cons new-wire (build-wires (cdr input-signals)))))) 
                 
 (define (get-signals wires) 
       (map (lambda (w) (get-signal w)) wires)) 
  
  
 ;;biggest digit first 
 (define (to-binary number) 
         (if  (< number 2) 
              (list number) 
              (cons (mod number 2) (to-binary (div number 2)) ))) 
  
 ;;simulation 
 (define the-agenda (make-agenda)) 
 (define inverter-delay 1) 
 (define and-gate-delay 1) 
 (define or-gate-delay 1) 
  
 (define a (build-wires '(0 0 1 0 0 0))) ;;4 
 (define b (build-wires '(1 1 0 1 0 0))) ;;11 
 (define s (build-wires '(0 0 0 0 0 0))) 
 (define c-in (make-wire)) 
 (ripple-carry-adder a b c-in s) 
  
 (propagate) 
 (get-signals s) ;;should be 15 

Added a couple of convenience procedures if someone wants to play with adders and multipliers