Higher-Order Programming » Lab Sessions

Session 7 (Solutions)

Assistant: Steven Keuchel (steven.keuchel@vub.be)

Section 2: Scheme Objects

Exercise 1: 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





Fibonacci:





(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.

An alternative solution is to turn an open recursive generator into a monitored function