x = 0
foo = function(){ x += 1 }
bar = function(){ x -= 1 }
Somehow you need to update bar's x too when you call foo and foo's x when you call bar. One way to do this by wrapping x in an object of its own, for example in a single element array that just contains x. I think the implementation of closures in Mono got this wrong, calling foo would have no effect on bar and vice versa (perhaps it's fixed now).The best way to treat this is to view closed variables as global variables in different scopes. Global variables imply they are shared.
e.g.
x = 1 // environment in the global scope
function baz()
// environment in baz's scope shared by foo and bar
x = 3
foo = function() { x += 1 }
bar = function(){ x -= 1 }
return [foo, bar]
foo1, bar1 = baz() // create a baz_env1 [x:3]
bar1() // baz_env1 [x:2]
foo1() // baz_env1 [x:3]
foo1() // baz_env1 [x:4]
foo2, bar2 = baz() // create a baz_env2 [x:3]
bar2() // baz_env2 [x:2]
x = 10
// At this point,
// global environment's x is 10
// baz_env1 x is 4
// baz_env2 x is 2
Hope that clarifies things.Is that correct?
* Compilers/interpreters are free to do optimizations that won't affect results, like only heap-allocating the closures that escape their scope.
Many concepts really aren't that complicated if you're not worried about optimization, and getting to a working model tends to shake out the aspects you don't completely understand. (This is also why books have exercises.)
That's my 'I have understood' benchmark. If I can re-implement something without any reference to outside sources.
My goal is to write a little lisp or scheme from the ground up from nothing but the lambda calculus and some bits & pieces. It'll be slow as molasses but it will help me to really grok it.
That's why I've been reading up on all this stuff over the past year or so. After that I think I'm ready for something more involved, like clojure. I realize that this is not a very efficient way of learning but in the long run it tends to pay off. Knowing what goes on under the hood is an advantage.
You'll probably want to get Quinniec's _Lisp in Small Pieces_, if you don't have it already. It covers several aspects of implementing Lisp interpreters and compilers, the lambda calculus, a bit on semantics, etc. (It mostly focuses on Scheme.)
That's closure. Easy, right?