<< Previous exercise (2.97)
| Index |
Next exercise (3.2) >>
The solution here presents itself: use lexical closure.
(define (make-accumulator initial-value)
(let ((sum initial-value))
(set! sum (+ sum n))
Lexical closure is indeed the way to go.
You can also use the trick that the initial value passed as parameter is already part of the lexical scope to save an instruction and write the accumulator as:
(define (make-accumulator acc)
(set! acc (+ x acc))