SICP Solutions

Chapter 3, Modularity, Objects, and State

Exercise 3.30

Note that from this exercise onwards, I shifted to MIT-scheme. There were two reasons behind it - (i) I recently started learning emacs and want to use it here (ii) Sometimes there are few cases which were not exactly same in Dr Racket sicp language package as expected - eg: mcons.

Assuming that list contains bits ordered as A1, A2 … i.e. first element of list contains the first bit and so on.

Note while adding last elements An and Bn, we need to send c-in = 0 in the adder.

Thus addition process starts from the last element/bit and the output carry is then fed as c-in in full adder while adding penultimate element/bit and then the carry generated is fed into full adder for adding last second bits and so on till we reach the first element.

And when we add the first element the carry generated by this full-adder becomes the final carry C from adding n bits.

Note that the mutability has caused to write the expressions in a certain order - we need the carry set/assigned by ripple-carry-adder before calling full-adder.

We can not test this code at this point in the book because agenda data-structure is not yet available. Later after this was introduced, I completed the code and putting the complete code here:

And now we can test the code (Adding As = 1 1 1 and Bs = 1 1 1 results Ss = 110 and C = 1):