| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The three forms differ in terms of the scope exprs are evaluated.
Let evaluates exprs before (outside of) let form.
Let* evaluates exprs in the scope where vars before
it are bound. Letrec evaluates exprs in the environment
where vars are already bound (to an undefined value, initially).
letrec is necessary to define mutually recursive local procedures.
(define x 'top-x)
(let ((x 3) (y x)) (cons x y)) => (3 . top-x)
(let* ((x 3) (y x)) (cons x y)) => (3 . 3)
(let ((cons (lambda (a b) (+ a b)))
(list (lambda (a b) (cons a (cons b 0)))))
(list 1 2)) => (1 2 . 0)
(letrec ((cons (lambda (a b) (+ a b)))
(list (lambda (a b) (cons a (cons b 0)))))
(list 1 2)) => 3
|
(let ((var expr)) body ...) |
(define (divrem n m) (values (quotient n m) (remainder n m))) (receive (q r) (divrem 13 4) (list q r)) => (3 1) (receive all (divrem 13 4) all) => (3 1) (receive (q . rest) (divrem 13 4) (list q rest) => (3 (1)) |
See also call-with-values in 6.15.4 Multiple values
which is the procedural equivalent of receive.
You can use define-values (See section 4.10 Definitions) to
bind multiple values to the toplevel variables simultaneously.
Also let-values and let*-values
in SRFI-11 (10.6 srfi-11 - Let-values) provides
let-like syntax with multiple values.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |