dynamic-scope


(This is just a start. Please feel free to refactor).

Scoping rules describe how the interpreter identifies the particular binding that is to be associated with a variable reference. Under lexical-scope, which is what Scheme follows, the interpreter searches in textually (or lexically) surrounding definitions (lambdas and lets). In contrast, dynamic scope dictates that the interpreter search in the frames of functions that called the function in which the variable reference appears. Of course, since the chain of calls can in general be different on every call, there is no way to statically associate (hence the name "dynamic") a variable reference with a declaration.

To put it in another way: Observe that every point in a program is associated with two "chains". One of these chains is the chain of lexically enclosing definitions, or "enclosing environments" (as described in sicp). The other is the chain of activation records, that is, the chain of stack frames pending at that point. Under lexical scoping the interpreter searches for a variable definition in the chain of environments, under dynamic scope it searches in the chain of activation records.

Early Lisp dialects almost always had dynamic scope since it is easier to implement than lexical scope. Common Lisp uses lexical scope by default, but some variables can be made to obey dynamic scope ("special" variables).

Dynamic scope is also historically important because defining recursive functions does not require any special magic like the y-combinator under dynamic scope, unlike lexical-scope.


category-learning-scheme