[previous] [up] [next]     [index]
Next: Assignments Up: Binding Forms Previous: Global Variables

Local Variables

Local variables are bound with standard Scheme's let, let*, and letrec. MzScheme's letrec form guarantees sequential evaluation of the binding expressions.

Multiple values are bound to multiple local variables at once with let-values, let*-values, and letrec-values. The syntax for let-values is:

  (let-values (((variable  tex2html_wrap_inline100181 ) expr)  tex2html_wrap_inline100181 ) body-expr  tex2html_wrap_inline100183 ) 
As in define-values, the number of values returned by each expr must match the number of variables declared in the corresponding clause. Each expr remains outside of the scope of all variables bound by the let-values expression.

The syntax for let*-values and letrec-values is the same as for let-values, and the binding semantics for each form corresponds to the single-value binding form:

When a letrec or letrec-values expression is evaluated, each variable binding is initially assigned the special undefined value (see section 4.1); the undefined value is replaced once the corresponding expression is evaluated.

Examples:

  (define x 0) 
  (let ([x 5] [y x]) y) ; => 0 
  (let* ([x 5] [y x]) y) ; => 5 
  (letrec ([x 5] [y x]) y) ; => 5 
  (letrec ([x y] [y 5]) x) ; => undefined 
  (let-values ([(x) 5] [(y) x]) y) ; => 0 
  (let-values ([(x y) (values 5 x)]) y) ; => 0 
  (let*-values ([(x) 5] [(y) x]) y) ; => 5 
  (let*-values ([(x y) (values 5 x)]) y) ; => 0 
  (letrec-values ([(x) 5] [(y) x]) y) ; => 5 
  (letrec-values ([(x y) (values 5 x)]) y) ; => undefined 
  (letrec-values ([(odd even) 
                   (values 
                    (lambda (n) (if (zero? n) #f (even (sub1 n)))) 
                    (lambda (n) (if (zero? n) #t (odd (sub1 n)))))]) 
     (odd 17)) ; => #t 

[previous] [up] [next]     [index]
Next: Assignments Up: Binding Forms Previous: Global Variables

PLT