Variable Bindings and Scope

In Interlisp, SETQ creates a persistent or global binding that keeps its value across computations. In contrast, LET and LET* create temporary, local variables that exist only within the current expression or function. LET evaluates and binds all variables in parallel, so none of them can refer to another defined in the same form. LET*, however, binds variables one at a time in sequence, allowing each new variable to use the values of those bound earlier, making it useful for stepwise calculations or dependent local values.

Form

SETQ

Scope

Global Binding

Example
  1. (SETQ TEMP1 (IPLUS 5 5))

  2. (SETQ RESULT (ITIMES 2 TEMP1))

  3. (SETQ GLOBAL.VALUE TEMP1)

  4. RESULT

Comments by Line Number

1. Global assignment: TEMP1 is now 10. 2. Global assignment: RESULT is now 20. 3. Global assignment: GLOBAL.VALUE is now 10. 4. Returns the current value of RESULT (20).

Form

LET

Scope

Local, Parallel Binding

Example
  1. (LET ((TEMP1 (IPLUS 5 5))

  2. (RESULT 20))

  3. (SETQ GLOBAL.VALUE TEMP1)

  4. (ITIMES RESULT 2))

Comments by Line Number
  1. Local bindings: TEMP1 = 10

  2. RESULT = 20 (Evaluated in parallel. Cannot use TEMP1 here.)

  3. Global value of GLOBAL.VALUE set to 10.

  4. Returns (ITIMES RESULT 2) = 40.

Form

LET*

Scope

Local, Sequential Binding

Example
  1. (LET* ((TEMP1 (IPLUS 5 5))

  2. (RESULT (ITIMES 2 TEMP1)))

  3. (SETQ GLOBAL.VALUE TEMP1)

  4. RESULT)

Comments by Line Number
  1. Local binding: TEMP1 = 10

  2. RESULT = (ITIMES 2 TEMP1) = 20

  3. Global value of GLOBAL.VALUE set to 10.

  4. Returns local RESULT = 20.

Last updated

Was this helpful?