Define a global variable named *counter*, giving it the initial
value 0. This variable will be used in subsequent exercises.
(When a Scheme programmer expects that the value of a global variable will be changed by assignments, she conventionally gives it a name beginning and ending with a star. This is not a requirement of the language, but a convention that makes programs more readable.)
Write a procedure named bump-counter! that takes no arguments and is
invoked for its side effect, which is to increase the value of *counter* by 1.
Write a procedure named reset-counter! that takes no arguments and is
invoked for its side effect, which is to change the value of *counter* to 0.
Write a procedure named report-counter that takes no
arguments and returns the current value of *counter*.
> (bump-counter!) > (report-counter) 1 > (bump-counter!) > (bump-counter!) > (bump-counter!) > (report-counter) 4 > (reset-counter!) > (report-counter) 0
Define a procedure named invocations that takes no arguments and
returns a natural number indicating the number of times it has been
invoked. (Use a static variable to keep track of this number.)
> (invocations) 1 > (invocations) 2 > (invocations) 3 > (invocations) 4
Define a procedure named parm that takes either one argument or none
and controls one static variable. Given no arguments, parm should
return the value of its static variable; given one argument, it should
assign the argument to the static variable, returning an unspecified value.
Define a unary procedure named list-keeper. Given any argument
other than the symbol :report, list-keeper should return the
symbol ok. When the argument is :report, however, list-keeper should return a list of the arguments from all of the
invocations of it since the last one with the :report argument:
> (list-keeper ':report) () > (list-keeper 'start) ok > (list-keeper 'next) ok > (list-keeper 2) ok > (list-keeper "three") ok > (list-keeper 'finish) ok > (list-keeper ':report) (start next 2 "three" finish) > (list-keeper 'start-again) ok > (list-keeper 'keep-going) ok > (list-keeper ':report) (start-again keep-going)
Hint: Save the arguments of the non-report calls in a list that is the value of a static variable.
Design and write a procedure direction-counter that takes one
argument, which must be one of the five symbols north, east,
south, west, or :report. Given any of the first four
symbols, it should return the symbol ok; given the symbol :report, it should display an appropriately formatted summary of the
number of times it has been invoked with each argument since the last
report.
> (direction-counter ':report) north 0 east 0 south 0 west 0 > (direction-counter 'east) ok > (direction-counter 'east) ok > (direction-counter 'south) ok > (direction-counter 'east) ok > (direction-counter 'north) ok > (direction-counter 'south) ok > (direction-counter 'east) ok > (direction-counter ':report) north 1 east 4 south 2 west 0