HOP WPO 4 (Solutions)
Assistant: Bjarno Oeyen (email@example.com)
- Use the same visual notation for drawing
- Note that in the solutions to these exercises, I have annotated procedure applications with how they are called, and their results. However, this is not required.
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
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:
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.
Send me an email if you want to check your solution of the bonus exercises!