灯下 登录
计算机科学 / SICP / 3.1.1 Local State Variables

Exercise 3.1 · 习题

Exercise 3.1: 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
generates accumulators, each maintaining an independent 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

练习 3.1:累加器 (accumulator) 是一个过程,它被反复调用,每次接受一个数值参数,并将其参数累加到一个总和中。每次调用时,它返回当前已累加的总和。编写过程 make-accumulator,用于生成累加器,每个累加器维护一个独立的总和。make-accumulator 的输入应指定总和的初始值;例如

(define A (make-accumulator 5))

(A 10)
15

(A 10)
25

Racket #lang sicp
(define A (make-accumulator 5))

(A 10)
15

(A 10)
25