(define (make-monitored f) (define num-calls 0) (lambda (x) (cond ((eq? x 'how-many-calls?) num-calls) ((eq? x 'reset-count) (set! num-calls 0)) (else (set! num-calls (+ num-calls 1)) (f x))))) ; 1 ]=> (define s (make-monitored sqrt)) ; ; ;Value: s ; ; 1 ]=> (s 100) ; ; ;Value: 10 ; ; 1 ]=> (s 'how-many-calls?) ; ; ;Value: 1 ; ; 1 ]=> (s 9) ; ; ;Value: 3 ; ; 1 ]=> (s 625) ; ; ;Value: 25 ; ; 1 ]=> (s 'how-many-calls?) ; ; ;Value: 3 ; ; 1 ]=> (s 'reset-count) ; ; ;Value: 3 ; ; 1 ]=> (s 'how-many-calls?) ; ; ;Value: 0 ; ; 1 ]=> (define (fib n) (if (< n 2) n (+ (fib (- n 1)) (fib (- n 2))))) ; ; ;Value: fib ; ; 1 ]=> (define f (make-monitored fib)) ; ; ;Value: f ; ; 1 ]=> (f 6) ; ; ;Value: 8 ; ; 1 ]=> (f 'how-many-calls?) ; ; ;Value: 1 ; note: monitor does not intercept recursive calls