Global variables have values that last until explicitly superseded with new values. Sometimes it is useful to create variable values that exist temporarily—only until a certain part of the program finishes. These values are called local, and the variables so used are called local variables.
For example, when a function is called, its argument variables receive
new local values that last until the function exits. The
special form explicitly establishes new local values for specified
variables; these last until exit from the
Establishing a local value saves away the variable's previous value (or lack of one). We say that the previous value is shadowed and not visible. Both global and local values may be shadowed (see Scope). After the life span of the local value is over, the previous value (or lack of one) is restored.
If you set a variable (such as with
setq) while it is local,
this replaces the local value; it does not alter the global value, or
previous local values, that are shadowed. To model this behavior, we
speak of a local binding of the variable as well as a local value.
The local binding is a conceptual place that holds a local value.
Entering a function, or a special form such as
let, creates the
local binding; exiting the function or the
let removes the
local binding. While the local binding lasts, the variable's value is
stored within it. Using
set while there is a
local binding stores a different value into the local binding; it does
not create a new binding.
We also speak of the global binding, which is where (conceptually) the global value is kept.
A variable can have more than one local binding at a time (for
example, if there are nested
let forms that bind it). In such a
case, the most recently created local binding that still exists is the
current binding of the variable. (This rule is called
dynamic scoping; see Variable Scoping.) If there are no
local bindings, the variable's global binding is its current binding.
We sometimes call the current binding the most-local existing
binding, for emphasis. Ordinary evaluation of a symbol always returns
the value of its current binding.
The special forms
let* exist to create
This special form binds variables according to bindings and then evaluates all of the forms in textual order. The
let-form returns the value of the last form in forms.
Each of the bindings is either (i) a symbol, in which case that symbol is bound to
nil; or (ii) a list of the form
), in which case symbol is bound to the result of evaluating value-form. If value-form is omitted,
All of the value-forms in bindings are evaluated in the order they appear and before binding any of the symbols to them. Here is an example of this:
zis bound to the old value of
y, which is 2, not the new value of
y, which is 1.(setq y 2) ⇒ 2 (let ((y 1) (z y)) (list y z)) ⇒ (1 2)
This special form is like
let, but it binds each variable right after computing its local value, before computing the local value for the next variable. Therefore, an expression in bindings can reasonably refer to the preceding symbols bound in this
let*form. Compare the following example with the example above for
let.(setq y 2) ⇒ 2 (let* ((y 1) (z y)) ; Use the just-established value of
y. (list y z)) ⇒ (1 1)
Here is a complete list of the other facilities that create local bindings:
Variables can also have buffer-local bindings (see Buffer-Local Variables); a few variables have terminal-local bindings (see Multiple Terminals). These kinds of bindings work somewhat like ordinary local bindings, but they are localized depending on “where” you are in Emacs, rather than localized in time.
This variable defines the limit on the total number of local variable bindings and
unwind-protectcleanups (see Cleaning Up from Nonlocal Exits) that are allowed before Emacs signals an error (with data
"Variable binding depth exceeds max-specpdl-size").
This limit, with the associated error when it is exceeded, is one way that Lisp avoids infinite recursion on an ill-defined function.
max-lisp-eval-depthprovides another limit on depth of nesting. See Eval.
The default value is 1000. Entry to the Lisp debugger increases the value, if there is little room left, to make sure the debugger itself has room to execute.