;; Exercise 3.2: 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 itself ;; takes one input. The result returned by 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 how-many-calls?, then mf returns the value of ;; the counter. If the input is the special symbol reset-count, then ;; mf resets the counter to zero. For any other input, mf returns the ;; result of calling f on that input and increments the counter. For ;; instance, we could make a monitored version of the sqrt procedure: ;; (define s (make-monitored sqrt)) (s 100) ;; 10 ;; (s ’how-many-calls?) ;; 1 ;; ----------------------------------- ;; We did this together in class on Oct 21. ;; ;; $ scheme 3-2.scm ;; (msqrt 100) is 10. ;; (msqrt 16) is 4. ;; (msqrt 'how-many-calls?) is 2. (define (make-monitored f) (let ((counter 0)) (lambda (x) ;; monitored version of f (cond ((eq? x 'reset) (set! counter 0)) ((eq? x 'how-many-calls?) counter) (else (begin (set! counter (+ 1 counter)) (f x))))))) (define msqrt (make-monitored sqrt)) (printf "(msqrt 100) is ~s.\n" (msqrt 100)) (printf "(msqrt 16) is ~s.\n" (msqrt 16)) (printf "(msqrt 'how-many-calls?) is ~s.\n" (msqrt 'how-many-calls?))