Posts

Showing posts from November, 2018

SICP Exercise 3.9 factorial environment structures

Image
Exercise 3.9.    In section  1.2.1  we used the substitution model to analyze two procedures for computing factorials, a recursive version (define (factorial n)   (if (= n 1)       1       (* n (factorial (- n 1))))) and an iterative version (define (factorial n)   (fact-iter 1 1 n)) (define (fact-iter product counter max-count)   (if (> counter max-count)       product       (fact-iter (* counter product)                  (+ counter 1)                  max-count))) Show the environment structures created by evaluating  (factorial 6)  using each version of the  factorial  procedure. 14 SOLUTION The worked out sheets are here: Page 1 Page 2 Page 3

SICP Exercise 3.8 evaluation order

Exercise 3.8.    When we defined the evaluation model in section  1.1.3 , we said that the first step in evaluating an expression is to evaluate its subexpressions. But we never specified the order in which the subexpressions should be evaluated (e.g., left to right or right to left). When we introduce assignment, the order in which the arguments to a procedure are evaluated can make a difference to the result. Define a simple procedure  f  such that evaluating  (+ (f 0) (f 1))  will return 0 if the arguments to  +  are evaluated from left to right but will return 1 if the arguments are evaluated from right to left. SOLUTION The code is here: Exercise 3.8 evaluation order Everything is good but the state variables are not properly encapsulated. Spent some time on this but could not find a way to hide them.

SICP Exercise 3.7 make-joint

Exercise 3.7.    Consider the bank account objects created by  make-account , with the password modification described in exercise  3.3 . Suppose that our banking system requires the ability to make joint accounts. Define a procedure  make-joint  that accomplishes this.  Make-joint  should take three arguments. The first is a password-protected account. The second argument must match the password with which the account was defined in order for the  make-joint  operation to proceed. The third argument is a new password.  Make-joint is to create an additional access to the original account using the new password. For example, if  peter-acc  is a bank account with password  open-sesame , then (define paul-acc   (make-joint peter-acc 'open-sesame 'rosebud)) will allow one to make transactions on  peter-acc  using the name  paul-acc  and the password  rosebud . You may wish to modify your solution to exercise  3.3  to accommodate this new feature. SOLUTION The code is here:

SICP Exercise 3.6 reset random number generator

Exercise 3.6.    It is useful to be able to reset a random-number generator to produce a sequence starting from a given value. Design a new  rand  procedure that is called with an argument that is either the symbol  generate or the symbol  reset  and behaves as follows:  (rand 'generate)  produces a new random number;  ((rand 'reset) < new-value >)  resets the internal state variable to the designated < new-value >. Thus, by resetting the state, one can generate repeatable sequences. These are very handy to have when testing and debugging programs that use random numbers. SOLUTION The code is here: Exercise 3.6 reset random number generator.rkt