Sujet : Re: History of lexical scope in Lisp
De : 433-929-6894 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.lang.lispDate : 16. Mar 2024, 04:37:51
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20240315192646.51@kylheku.com>
References : 1
User-Agent : slrn/pre1.0.4-9 (Linux)
On 2024-03-15, Paul Rubin <
no.email@nospam.invalid> wrote:
Does anyone know when lexical scope started appearing in Lisp? Not
counting Scheme, did it exist in predecessors of Common Lisp? Was it
used much? Is it really true that Common Lisp had both lexical and
dynamic scope in order to support older code that was written relying on
dynamic scope and was too hard to convert?
Global variables are dynamically scoped in Common Lisp; the standard
language doesn't provide for global variables that are not dynamically
scoped: "dynamic variable", "global variable" and "top-level variable"
refer to the same thing. Common Lisp's lexical scope design therefore
isn't "total" in a sense; it doesn't provide top-level variables.
New Common Lisp code continues to make use of dynamic scope.
Important predefined variables such as the standard streams (e.g.
*standard-output*) are dynamic variables. By locally binding
*standard-output* we can redirect standard output to a different
stream.
In multithreaded Common Lisp implementations, dynamically scoped
variables provide thread-local storage (as an extension to the
language). If each thread can have its own independent binding of a
dynamic variable, it means that the symbol serves as a key
to a thread-specific storage location.
There are good reasons to use dynamic variables in newly written Lisp
code.
For instance, a context handle (e.g. database handle) that rarely
changes and that is passed around in numerous API calls can be a special
variable, and then it doesn't have to be repated in all those calls.
Yet if it has to take on different values, there is a clean way to do
that.
The aforementioned standard streams are such context handles. For
instance when we call (write-strin "abc"), the default destination is
*standard-output*; it need not be mentioned, if desired. The function
will use the current value of the dynamic variable *standard-output*.
-- TXR Programming Language: http://nongnu.org/txrCygnal: Cygwin Native Application Library: http://kylheku.com/cygnalMastodon: @Kazinator@mstdn.ca