3.2 Exercise 3.2
In this exercise, we create a procedure that decorates a given procedure and maintains an inner accumulator (possibly created using make-accumulator but in this case not) which it uses to count how many times the decorated procedure has been called. Additionally, you can pass the argument of ’how-many-calls? to get the current value of the counter, and reset-count to set the counter to 0.
Although the given example of decorating sqrt doesn’t suggest it, it’s reasonable to expect that we should allow for decorating procedures of any number of arguments, rather than just one. This implementation handles messages in the simplest way in that it only checks if the first argument is equal to a special message. If so, the other arguments are ignored.
(define (make-monitored f) (let ((count 0)) (lambda args (cond ((eq? 'how-many-calls? (car args)) count) ((eq? 'reset-count (car args)) (set! count 0)) (else (begin (set! count (+ count 1)) (apply f args)))))))