4 ms·
Lexical scoping is a function of space (lexical context) and dynamic scoping is a function of time. Consider: (let ((x 'lexical)) (let ((f (lambda () x))
by nils-m-holm 8y ago
Lexical scoping is a function of space (lexical context) and dynamic scoping is a function of time. Consider:
(let ((x 'lexical))
(let ((f (lambda () x)))
(let ((x 'dynamic))
(f))))
In a lexically scoped system, X is bound to LEXICAL and then F is bound to a function returning X in the lexical context where X=LEXICAL. Then (another) X is bound to DYNAMIC and finally F is applied. The value that F returns is the value that X had in the lexical/spatial context of the definition of F.
In a dynamically scoped system, F is bound to a function that returns the value that X is currently bound to. Before the function is defined, X is bound to LEXICAL and later the same X is bound to a different value, DYNAMIC. The value returned by F is the value that X has at the time of the function application.
To try this in a dynamically scoped LISP, use may have to use
(funcall f)
instead of (f) in the last line of the example.