Binding constructs
The binding constructs
(let
{bindings} {body}
)
syntax
Syntax:
Bindings has the form
(({variable1} {init1}) ...)
where each init is an expression, and body is a sequence of zero or more definitions followed by a sequence of one or more expressions as described in section 4.1.4. It is an error for a variable to appear more than once in the list of variables being bound. Semantics: The inits are evaluated in the current environment (in some unspecified order), the variables are bound to fresh locations holding the results, the body is evaluated in the extended environment, and the values of the last expression of body are returned. Each binding of a variable has body as its region.
(let ((x 2) (y 3))
(* x y)) ==> 6
(let ((x 2) (y 3))
(let ((x 7)
(z (+ x y)))
(* z x))) ==> 35
See also "named
(let*
{bindings} {body}
)
syntax
Syntax:
Bindings has the form
(({variable1} {init1}) ...)
and body is a sequence of zero or more definitions followed by one or more expressions as described in section 4.1.4.
Semantics:
The
(let ((x 2) (y 3))
(let* ((x 7)
(z (+ x y)))
(* z x))) ==> 70
(letrec
{bindings} {body}
)
syntax
Syntax:
Bindings has the form
(({variable1} {init1}) ...)
and body is a sequence of zero or more definitions followed by one or more expressions as described in section 4.1.4. It is an error for a variable to appear more than once in the list of variables being bound.
Semantics:
The variables are bound to fresh locations holding unspecified
values, the inits are evaluated in the resulting environment (in
some unspecified order), each variable is assigned to the result
of the corresponding init, the body is evaluated in the
resulting environment, and the values of the last expression in
body are returned. Each binding of a variable has the
entire
(letrec ((even?
(lambda (n)
(if (zero? n)
#t
(odd? (- n 1)))))
(odd?
(lambda (n)
(if (zero? n)
#f
(even? (- n 1))))))
(even? 88))
==> #t
(letrec*
{bindings} {body}
)
syntax
Syntax:
Bindings has the form
(({variable1} {init1}) ...)
and body is a sequence of zero or more definitions followed by one or more expressions as described in section 4.1.4. It is an error for a variable to appear more than once in the list of variables being bound.
Semantics:
The variables are bound to fresh locations,
each variable is assigned in left-to-right order to the
result of evaluating the corresponding init, the body is
evaluated in the resulting environment, and the values of the last
expression in body are returned.
Despite the left-to-right evaluation and assignment order, each binding of
a variable has the entire If it is not possible to evaluate each init without assigning or referring to the value of the corresponding variable or the variable of any of the bindings that follow it in bindings, it is an error. Another restriction is that it is an error to invoke the continuation of an init more than once.
(letrec* ((p
(lambda (x)
(+ 1 (q (- x 1)))))
(q
(lambda (y)
(if (zero? y)
0
(+ 1 (p (- y 1))))))
(x (p 5))
(y x))
y)
==> 5
(let-values
{mv binding spec} {body}
)
syntax
Syntax:
{mv binding spec} has the form
(({formals1} {init1}) ...)
where each init is an expression, and body is zero or more definitions followed by a sequence of one or more expressions as described in section 4.1.4. It is an error for a variable to appear more than once in the set of formals.
Semantics:
The inits are evaluated in the current environment (in some
unspecified order) as if by invoking It is an error if the formals do not match the number of values returned by the corresponding init.
(let-values (((root rem) (exact-integer-sqrt 32)))
(* root rem)) ==> 35
(let*-values
{mv binding spec} {body}
)
syntax
Syntax:
{mv binding spec} has the form
(({formals} {init}) ...)
and body is a sequence of zero or more definitions followed by one or more expressions as described in section 4.1.4. In each formals, it is an error if any variable appears more than once.
Semantics:
The
(let ((a 'a) (b 'b) (x 'x) (y 'y))
(let*-values (((a b) (values x y))
((x y) (values a b)))
(list a b x y))) ==> (x y x y)
|