The 'let' special form is basically a local block construct that contains symbols [with optional initializations] and a block of code [expressions] to evaluate. The first form after the 'let' is the 'binding' form. It contains a series of 'symbols' or 'bindings'. The 'binding' is a 'symbol' followed by an initialization expression 'init-expr'. If there is no 'init-expr', the 'symbol' will be initialized to NIL. There is no specification as to the order of execution of the bindings. The 'let' form will go through and create and initialize the symbols and then sequentially execute the 'exprs'. The value of the last 'expr' evaluated is returned. When the 'let' is finished execution, the 'symbols' that were defined will no longer exist or retain their values.
(let (x y z) ; LET with local vars (print x) (print y) (print z)) ; prints NIL NIL NIL (let ((a 1) (b 2) (c 3)) ; LET with local vars & init (print (+ a b c))) ; prints and returns 6 (let ((a 1) (b 2) (c (+ a b))) ; LET with local vars & init (print (+ a b c))) ; error: unbound variable - A
Note: to make the last example work you need the let* special form.
See the
let
special form in the