HOP WPO 4
Assistant: Bjarno Oeyen (firstname.lastname@example.org)
Section 1: Environment Diagrams
Exercise 1: A Functional Environment Diagram
Using pen and paper, draw an environment diagram of evaluating
(square 5) and
(f 5) using the following definitions. You do not need to draw the definition of these 3 procedures.
Exercise 2: A Recursive Environment Diagram
The following is a solution of an earlier exercise.
Draw an environment diagram when evaluating
(add-to-end (list 1 2 3) 5).
Exercise 3: A Lexical Environment Diagram
Using pen and paper, draw an environment diagram of evaluating the following program.
What is the displayed in the REPL? Is this the result you expected? What would be the result if Scheme had a dynamic scope (instead of a lexical scope)?
Section 2: Scheme Objects
Exercise 4: The Flip Procedure
- Define a procedure
flip(with no parameters) that returns 1 the first time it is called, 0 the second time it is called, 1 the third time, 0 the fourth time, and so on.
- Define a procedure
make-flipthat can be used to generate flip procedures. That is, we should be able to define
(define flip (make-flip)).
- Draw an environment diagram to illustrate the result of evaluating the following sequence of expressions.
Exercise 5: A Turnstile
Create a Scheme object (see bank account example in the theory) that implements the behaviour of a turnstile (e.g., the barriers found at metro stops, the resto…). They count the number of persons that pass a specific point.
They should support the following operations.
- Incrementing the counter of the turnstile (using a given increment count)
- Querying the current counter of the turnstile.
The following boilerplate code is given.
Draw the environment diagram of executing the following sequence of expressions.
Exercise 6: Function Profiling
In software-testing applications, it is useful to be able to count the number of times a given procedure is called during the course of a computation. Write a procedure
make-monitored that takes as input a procedure
f, that takes itself one input. The result returned by applying
make-monitored is a third procedure, say
mf, that keeps track of the number of times it has been called by maintaining an internal counter. If the input to
mf is the special symbol
mf returns the value of the counter. If the input is the special symbol
mf resets the counter to zero. For any other input,
mf returns the result of applying
f on that input and increments the counter. For instance, we could make a monitored version of the
What happens when you call a monitored version of a recursive definition of
Did your implementation return
1? Or another number? Adapt your solution. (Hint! You don’t need to change
make-monitored. You have to find a way to redefine
Bonus Exercise 1: Assignments
Recommended after exercise 3.
Predict the outcome of evaluating the following expressions in the Read-Eval-Print-Loop.
(= a 5)
(define a 12)
(define b 10)
(= a 5)
(set! a 5)
(= a 12)
(= b a)
(set! b a)
(= a b)
(define c a)
(set! a 100)
(= a c)
Explain these results. You can draw an environment diagram to help you explain these results.
Bonus Exercise 2: A Turnstile Extension
Recommended after exercise 5.
Extend the implementation for turnstile with the following features.
- Resetable: make it possible to reset the turnstile to 0
- Maximum count: if more than \(x\) persons passed: block the turnstile
- Must be configurable.
- When incrementing: return
#fwhen there is not enough room.
- Alert: If more than \(y\) persons passed: display a warning message.
- The amount must be configurable.
- The message must also be configurable.
Bonus Exercise 3: My Own Pairs
Recommended after exercise 6.
A definition for
cdr is shown below that utilises
Draw the environment diagram of executing the following program.