# SICP Solutions

### Chapter 5, Computing with Register Machines

#### Exercise 5.25

This exercise turned out quite interesting. It displays that writing code in machine instructions is not that difficult. It also shows why even it being not difficult - it’s tedious.

Well, there’s a reason why we code in high level languages. For this small change, it took quite a bit of remembering - details of the values stored in each registers and stack saves and continue and so on.

Note again, after doing exercises of sec-4.2 in ch-4, this exercise in itself is not difficult but it requires a lot of remembering. I just replicated the exact flow as in the sec-4.2.

Few things to note for understanding the solution:

• Now, as in sec-4.2 lazy evaluator, we just dispatch the jump to apply-dispatch without evaluating the arguments.
• Now, apply-dispatch does following:
• if procedure is primitive then it jumps to ev-appl-operands-values where the operand values present in unev are evaluated and saved in argl. Note that how i saved proc and worked with continue to make sure that after argl is formed, we jump to primitive-apply with proc and argl available.
• if procedure is compound then it calls primitive procedure list-of-delayed-args to convert all the arguments into thunks.
• Defined a label actual-value, that works exactly like procedure actual-value in our lazy evaluator of sec-4.2. It evaluates the expression in exp and then in loop it keeps evaluating the result until the result is no longer a thunk. At this point it jumps back to the original continue(thus we do some saving of continue here).
• Similar to the lazy-evaluator, i changed if, cond and the main drive loop read-eval-print-loop to use actual-value instead of eval-dispatch.

And that’s it!

First I shall preesent the important changes then at the bottom I shall put the complete code of the evaluator:

(Above code does not contain changes in ev-if, ev-cond and read-eval-print-loop - check complete code at the bottom to view those changes)

There are few primitive procedures which were added in ch5-eceval-support.scm:

Example/Output

First few basic tests:

Now, I tested with ex-4.27 - which demostrates that arguments are indeed delayed:

#### Complete Code

This contain all changes of ex-5.23, 5.24, 5.25

All the changes are mainly in file ch5-eceval.scm:(this file is origninally from mit website)

Note that there are few changes in file ch5-eceval-support.scm which i have shown above.

Apart from that, since above code also contain changes for ex-5.24, to run this code, few changes from ex-5.24 in file ch5-syntax.scm also needs to be included(or just remove the part corresponding to 5.23 and 5.24 in file ch5-eceval.scm shown above):