In this exercise, we are tasked with writing a procedure make-accumulator which can be used to generate accumulaors which can maintain their independant sum. It can be done using the following code by maintaining a local variable:-
Thus, an accumulator object is created.
Exercise 3.2
In this exercise, we are asked to write a procedure make-monitored which takes in a procedure f which itself takes in one input and returns a modified procedure mf. It can be used to keep track of the number of times the function was called. The following code allows us to achieve that by updating a local variable each time a function is called:-
Exercise 3.3
In this exercise, we need to modify the make-account procedure given in the book to take passowords.
Of course, passwords cannot be stored plainly as shown are usually stored hashed.
Exercise 3.4
In this exercise, we are told to extend the password protected transactions above to call the cops if the number of wrong tries consecutively exceeds 7. The following code does that:-
Each correct password resets the counter and each wrong password increments the calls counter. When it reaches 7, we call the cops even if right password is input.
Exercise 3.5
In this exercise, we are told to use the monte-carlo function given in the book to perform Monte Carlo integration. The function is as follows:-
We can reuse this function to perform the monte carlo integration using the following code:-
We can test this code by finding the area of a unit circle.
As can be seen we get a good approximation for $\pi$ which is the area of a unit circle. We can use any size of rectangle to compute this value.
Exercise 3.6
In this exercise we are tasked with writing a modified random-number generator that can have its seed set so that we can perform repeated experiments. It can be achieved using the following code:-
It uses a default seed value of 42 if no seed is set initially. As for the rand-update function, we use the Linear Congruential Generator.
The following tests can be run to verify the code:-
As can be seen, we can reset the stream of random numbers anytime to our chosen value.
Exercise 3.7
In this exercise, we are required to create a procedure make-account which can be used to create joint accounts. The joint account should be operated from the original account using the original password as well. We do that with the following code:-
As seen, both accounts share the same internal balance amount. Now, we can add more helpful error messages to users based on which password is wrong.
Exercise 3.8
In this exercise, we are told to create a function f which, when substituted into (+ (f 0) (f 1)) should evaluate to 0 when the subexpressions are evaluated from left to right and 1 otherwise. One such function is as follows:-
As can be seen, when (f 0) is evaluated first followed by (f 1) we get 0 and 0 which sum to 0. If (f 1) is evaluated first followed by (f 0), we get 1 and 0 which sum to 1. This fulfils the condition set forth.
We can check the actual order in mit-scheme and we get the folowing:-
Thus, it can be seen that the arguments to + are evaluated from right to left.