![[LISPWORKS]](/image.pl?url=http%3a%2f%2fwww.lispworks.com%2fdocumentation%2fHyperSpec%2fBody%2f..%2fGraphics%2fLWSmall.gif&f=jpg&w=240)
![[Common Lisp HyperSpec (TM)]](/image.pl?url=http%3a%2f%2fwww.lispworks.com%2fdocumentation%2fHyperSpec%2fBody%2f..%2fGraphics%2fCLHS_Sm.gif&f=jpg&w=240)
Syntax:
let({var | (var [init-form])}*)declaration*form* =>result*
let*({var | (var [init-form])}*)declaration*form* =>result*
Arguments and Values:
var---asymbol.
init-form---aform.
declaration---adeclareexpression; not evaluated.
form---aform.
results---thevalues returned by theforms.
Description:
let andlet* create new variablebindings and execute a series offorms that use thesebindings.let performs thebindings in parallel andlet* does them sequentially.
The form
(let ((var1 init-form-1) (var2 init-form-2) ... (varm init-form-m)) declaration1 declaration2 ... declarationp form1 form2 ... formn)first evaluates the expressionsinit-form-1,init-form-2, and so on, in that order, saving the resulting values. Then all of the variablesvarj are bound to the corresponding values; eachbinding is lexical unless there is aspecial declaration to the contrary. The expressionsformk are then evaluated in order; the values of all but the last are discarded (that is, the body of alet is animplicit progn).
let* is similar tolet, but thebindings of variables are performed sequentially rather than in parallel. The expression for theinit-form of avar can refer tovars previously bound in thelet*.
The form
(let* ((var1 init-form-1) (var2 init-form-2) ... (varm init-form-m)) declaration1 declaration2 ... declarationp form1 form2 ... formn)first evaluates the expressioninit-form-1, then binds the variablevar1 to that value; then it evaluatesinit-form-2 and bindsvar2, and so on. The expressionsformj are then evaluated in order; the values of all but the last are discarded (that is, the body oflet* is an implicitprogn).
For bothlet andlet*, if there is not aninit-form associated with avar,var is initialized tonil.
The special formlet has the property that thescope of the name binding does not include any initial value form. Forlet*, a variable'sscope also includes the remaining initial value forms for subsequent variable bindings.
Examples:
(setq a 'top) => TOP (defun dummy-function () a) => DUMMY-FUNCTION (let ((a 'inside) (b a)) (format nil "~S ~S ~S" a b (dummy-function))) => "INSIDE TOP TOP" (let* ((a 'inside) (b a)) (format nil "~S ~S ~S" a b (dummy-function))) => "INSIDE INSIDE TOP" (let ((a 'inside) (b a)) (declare (special a)) (format nil "~S ~S ~S" a b (dummy-function))) => "INSIDE TOP INSIDE"
The code
(let (x) (declare (integer x)) (setq x (gcd y z)) ...)is incorrect; althoughx is indeed set before it is used, and is set to a value of the declared typeinteger, neverthelessx initially takes on the valuenil in violation of the type declaration.
Affected By: None.
Exceptional Situations: None.
See Also:
Notes: None.