;; 3-1.scm ;; ;; An accumulator is a procedure that is called repeatedly with a ;; single numeric argument and accumulates its arguments into a ;; sum. Each time it is called, it returns the currently accumulated ;; sum. Write a procedure make-accumulator that gen- erates ;; accumulators, each maintaining an indepen- dent sum. The input to ;; make-accumulator should specify the initial value of the sum; for example ;; (define A (make-accumulator 5)) (A 10) ;; 15 ;; (A 10) ;; 25 ;; -------------- ;; ;; We did this together in class on Oct 21. ;; ;; $ scheme 3-1.scm ;; (define a1 (make-accumulator 10)) ;; (a1 5) is 15. ;; (a1 5) is 20. ;; (define (make-accumulator starting-amount) ;; return a function which ;; lets us add new amounts and ;; returns the new amount (lambda (amount) (set! starting-amount (+ amount starting-amount)) starting-amount)) (define a1 (make-accumulator 10)) (printf "(define a1 (make-accumulator 10))\n") (printf "(a1 5) is ~s.\n" (a1 5)) (printf "(a1 5) is ~s.\n" (a1 5))