【发布时间】:2012-06-05 23:27:10
【问题描述】:
我是 Scheme 的新手,并试图了解出现在函数中的某些值如何在多种用途中持续存在。采取以下柜台:
(define count
(let ((next 0))
(lambda ()
(let ((v next))
(set! next (+ next 1))
v))))
我无法弄清楚(也没有在任何地方找到解释),这就是为什么每次使用 count 时next 都不会重置为 0。
【问题讨论】:
-
这种现象也称为“静态变量”。请参阅 Scheme Cookbook 中的此食谱:schemecookbook.org/Cookbook/IdiomStaticVariables
标签: scheme closures mutators lexical-scope