3.6 Variables and Bindings

A variable is a place holder for a Standard LISP entity which is said to be bound to the variable. The scope of a variable is the range over which the variable has a defined value. There are three different binding mechanisms in Standard LISP.

Local Binding
This type of binding occurs only in compiled functions. Local variables occur as formal parameters in lambda expressions and as PROG form variables. The binding occurs when a lambda expression is evaluated or when a PROG form is executed. The scope of a local variable is the body of the function in which it is defined.
Global Binding
Only one binding of a global variable exists at any time allowing direct access to the value bound to the variable. The scope of a global variable is universal. Variables declared GLOBAL may not appear as parameters in lambda expressions or as PROG form variables. A variable must be declared GLOBAL prior to its use as a global variable since the default type for undeclared variables is FLUID.
Fluid Binding
Fluid variables are global in scope but may occur as formal parameters or PROG form variables. In interpreted functions all formal parameters and PROG form variables are considered to have fluid binding until changed to local binding by compilation. When fluid variables are used as parameters they are rebound in such a way that the previous binding may be restored. All references to fluid variables are to the currently active binding.

FLUID(IDLIST:id-list):NIL eval, spread
 
The ids in IDLIST are declared as FLUID type variables (ids not previously declared are initialized to NIL). Variables in IDLIST already declared FLUID are ignored. Changing a variable’s type from GLOBAL to FLUID is not permissible and results in the error:

***** ID cannot be changed to FLUID

FLUIDP(U:any):boolean eval, spread
 
If U has been declared FLUID (by declaration only) T is returned, otherwise NIL is returned.

GLOBAL(IDLIST:id-list):NIL eval, spread
 
The ids of IDLIST are declared global type variables. If an id has not been declared previously it is initialized to NIL. Variables already declared GLOBAL are ignored. Changing a variables type from FLUID to GLOBAL is not permissible and results in the error:

***** ID cannot be changed to GLOBAL

GLOBALP(U:any):boolean eval, spread
 
If U has been declared GLOBAL or is the name of a defined function, T is returned, else NIL is returned.

SET(EXP:id, VALUE:any):any eval, spread
 
EXP must be an identifier or a type mismatch error occurs. The effect of SET is replacement of the item bound to the identifier by VALUE. If the identifier is not a local variable or has not been declared GLOBAL it is automatically declared FLUID with the resulting warning message:

*** EXP declared FLUID

EXP must not evaluate to T or NIL or an error occurs:

***** Cannot change T or NIL

SETQ(VARIABLE:id, VALUE:any):any noeval, nospread
 
If VARIABLE is not local or GLOBAL it is by default declared FLUID and the warning message:

*** VARIABLE declared FLUID

appears. The value of the current binding of VARIABLE is replaced by the value of VALUE. VARIABLE must not be T or NIL or an error occurs:

***** Cannot change T or NIL

MACRO PROCEDURE SETQ(X);

  LIST(’SET, LIST(’QUOTE, CADR X), CADDR X);

UNFLUID(IDLIST:id-list):NIL eval, spread
 
The variables in IDLIST that have been declared as FLUID variables are no longer considered as fluid variables. Others are ignored. This affects only compiled functions as free variables in interpreted functions are automatically considered fluid [3].