Standard idiom in LISP (or in this case, Scheme),
Let over Lambda (also the title of an impressive book on LISP macros
by Doug Hoyte):
(let ((n 0))
(lambda () (begin
(set! n (+ 1 n))
What does it do? It returns a function that returns 1, 2, 3, ... when
called repeatedly. It's a way of keeping state in a world of functions.
Don't confuse it with C's
static Variables inside functions.
mkcounter constructs a new counter object for each invocation,
(define c1 (mkcounter))
(define c2 (mkcounter))
(c1) (c1) (c1) (c2)
An attempt to reconstruct this in Python 2.x:
n = 0
n = n+1
UnboundLocalError: local variable 'n' referenced before assignment,
which is somewhat confusing, since
n is visible in
n = n+1 line is removed.
The impossibility of LoL in Python has been pointed out in
PEP-3104 but was only fixed in Python 3.
In Python 3 it's possible to reconstruct LoL by the dubiously named
n = 0
n = n + 1