[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Making Lua lexically scoped (was: Re: Proper tail recursion)
- From: ramsdell@... (John D. Ramsdell)
- Date: 30 Jul 2001 10:25:21 -0400
Philippe Lhoste <PhiLho@gmx.net> writes:
> Still trying to learn (the easy way ;-):
> What do you mean by "referenced by a closure"?
> Actually, I am not sure to understand what a closure is. But I suppose it is
> like the upvalues: a view on the values of variables at function evaluation
> (by parser? by VM?) time.
> Then, referenced by a closure should be eg. the upvalue syntax.
Early versions of Lisp allowed code to be first class object, but used
the environment in which it was invoked to resolve variable references
to unbound variables. This is called dynamic scoping.
Some people thought dynamic scoping was a good idea, but others were
not so sure. The people that did not like dynamic scoping promoted
static scoping, which means that the variable binding to which a
variable reference refers can always be determined by a static
analysis of the source code. For example, C is statically scoped,
because at compile time, one can determing if each variable reference
refers to a global binding or a local one. Since C has only two
choices, this analysis is trivial, but this is not the case for a
lexically scoped language.
To create a lexically scoped Lisp, people found they needed to save
the environment of a code segment at the time the code object was
created. The combination of pure code and an environment is called a
closure. You can read all about closures and lexical scoping in
"Structure and intepretation of Computer Programs", by Sussman and
Abelson. The book is online at:
The chapter on "Computing with Register Machines" is a classic.