Next: , Previous: , Up: Variables   [Contents][Index]


16.3 Local Variables

Global variables have values that last until explicitly superseded with new values. Sometimes it is useful to create variable values that exist temporarily—only while within a certain part of the program. 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 let special form explicitly establishes new local values for specified variables; these last until exit from the let form.

Establishing a local value saves away the previous value (or lack of one) of the variable. When the life span of the local value is over, the previous value is restored. In the mean time, we say that the previous value is shadowed and not visible. Both global and local values may be shadowed (see Scope).

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. Entry to a function, or a special form such as let, creates the local binding; exit from the function or from the let removes the local binding. As long as the local binding lasts, the variable’s value is stored within it. Use of setq or 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 is called dynamic scoping; see Variable Scoping.) If there are no local bindings, the variable’s global binding is its current binding. We also 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 and let* exist to create local bindings.

Special Form: let (bindings…) forms…

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 (symbol value-form), in which case symbol is bound to the result of evaluating value-form. If value-form is omitted, nil is used.

All of the value-forms in bindings are evaluated in the order they appear and before any of the symbols are bound. Here is an example of this: Z is bound to the old value of Y, which is 2, not the new value, 1.

(setq Y 2)
     ⇒ 2
(let ((Y 1)
      (Z Y))
  (list Y Z))
     ⇒ (1 2)
Special Form: let* (bindings…) forms…

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). These kinds of bindings work somewhat like ordinary local bindings, but they are localized depending on “where” you are in SXEmacs, rather than localized in time.

Variable: max-specpdl-size

This variable defines the limit on the total number of local variable bindings and unwind-protect cleanups (see Nonlocal Exits) that are allowed before signaling 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.

The default value is 3000.

max-lisp-eval-depth provides another limit on depth of nesting. See Eval.


Next: , Previous: , Up: Variables   [Contents][Index]