« Return to index

HOP WPO 4 (Solutions)

Assistant: Bjarno Oeyen (bjarno.oeyen@vub.be)

General Remarks

Section 1: Environment Diagrams

Exercise 1: A Functional Environment Diagram

Exercise 2: A Recursive Environment Diagram

Exercise 3: A Lexical Environment Diagram

Section 2: Scheme Objects

Exercise 4: The Flip Procedure

Part 1:


Part 2:


Part 3:

Every call of make-flip produces a new lexical scope where a new state variable exists. Applying the flip procedure from this scope will result in it only modifying the state variable in that scope.

Exercise 5: A Turnstile


The turnstile is similar to that of the previous exercise. However, in this solution, there are multiple procedures that are created in the scope created by applying make-turnstile. It has 4 fields: counter, increment (a procedure object with scope), query (also a procedure object with scope), and dispatch (and also a procedure object with scope). All these procedures refer to the same lexical environment: thus the dispatch procedure has access to the the other closures.

Exercise 6: Function Profiling




(m-fib 'how-many-calls?) returns 1. However, we would expect more since the fib procedure is called multiple times recursively. A solution is to overwrite the definition of fib…


(m-fib 'how-many-calls?) now returns 15, which is the expected result.

Bonus Exercises

Send me an email if you want to check your solution of the bonus exercises!