2 ms·
Multiple closures can share the same state ("environment"). Consider: (let ((i 0)) (list (lambda () (incf i)) (lambda () (decf i)) (
by jrockway 17y ago
Multiple closures can share the same state ("environment"). Consider:
(let ((i 0))
(list (lambda () (incf i))
(lambda () (decf i))
(lambda () i)))
If you evaluate the first function this form returns ("increment") and then the third form ("get"), the result is 1. If you evaluate the whole form again, you now have another "instance", with its own separate shared i.
In Python, you would probably just make this a class. (But sometimes it's nice to have something equivalent to a class that's automatically built for you, and closures are that something.)