Local Binding

Local Binding

syntax: (let ((var val) ...) exp1 exp2 ...)
returns: the value of the final expression

let establishes local variable bindings. Each variable var is bound to the value of the corresponding expression val. The body of the let, in which the variables are bound, is the sequence of expressions exp1 exp2 ....

The forms let, let*, and letrec are similar but serve slightly different purposes.

With let, in contrast with let* and letrec, the expressions val ... are all outside the scope of the variables var .... Also, in contrast with let*, no ordering is implied for the evaluation of the expressions val .... They may be evaluated from left to right, from right to left, or in any other order at the discretion of the implementation. Use let whenever the values are independent of the variables and the order of evaluation is unimportant.

syntax: (let* ((var val) ...) exp1 exp2 ...)
returns: the value of the final expression

let* is similar to let except that the expressions val ... are evaluated in sequence from left to right, and each of these expressions is within the scope of the variables to the left. Use let* when there is a linear dependency among the values or when the order of evaluation is important.


syntax: (letrec ((var val) ...) exp1 exp2 ...)
returns: the value of the final expression

letrec is similar to let and let*, except that all of the expressions val ... are within the scope of all of the variables var .... letrec allows the definition of mutually recursive procedures.

The order of evaluation of the expressions val ... is unspecified, so it is an error to reference any of the variables bound by the letrec expression before all of the values have been computed.