def make_counter(): x = 0 def inc(): x = x + 1 return x return inc c1 = make_counter() c1() # returns 1
It might seem nice at first glance :)
But... oops! it doesn't work.
It doesn't work because the "x" in "x = x + 1" is _not_ the same "x" as in "x = 0".
And that happens because definition and assignment are badly mixed up.
(def make-counter () (let x 0 (fn () (= x (+ x 1)))))
-----