3.9 Local Binding: let, let*, letrec, ... (original) (raw)
3.9 Local Binding: let, let*, letrec, ...🔗ℹ
Local Binding in The Racket Guide introduces local binding.
(let ([id val-expr] ...) body ...+) (let proc-id ([id init-expr] ...) body ...+)
The first form evaluates the val-exprs left-to-right, creates a new location for each id, and places the values into the locations. It then evaluates the bodys, in which theids are bound. The last body expression is in tail position with respect to the let form. The ids must be distinct according to bound-identifier=?.
Examples:
> (let ([x 5]) x) 5 > (let ([x 5]) (let ([x 2] [y x]) (list y x))) '(5 2)
The second form, usually known as named let, evaluates the init-exprs; the resulting values become arguments in an application of a procedure(lambda (id ...) body ...+), where proc-id is bound within the bodys to the procedure itself.
Example:
(let* ([id val-expr] ...) body ...+)
Like let, but evaluates the val-exprs one by one, creating a location for each id as soon as the value is available. The ids are bound in the remaining val-exprs as well as the bodys, and the ids need not be distinct; later bindings shadow earlier bindings.
Example:
> (let* ([x 1] [y (+ x 1)]) (list y x)) '(2 1)
(letrec ([id val-expr] ...) body ...+)
Like let, including left-to-right evaluation of the val-exprs, but the locations for all ids are created first, allids are bound in all val-exprs as well as thebodys, and each id is initialized immediately after the corresponding val-expr is evaluated. The ids must be distinct according tobound-identifier=?.
Referencing or assigning to an id before its initialization raises exn:fail:contract:variable. If an id (i.e., the binding instance or id) has an'undefined-error-name syntax property whose value is a symbol, the symbol is used as the name of the variable for error reporting, instead of the symbolic form of id.
Example:
Changed in version 6.0.1.2 of package base: Changed reference or assignment of an uninitialized id to an error.
(let-values ([(id ...) val-expr] ...) body ...+)
Likelet, except that each val-expr must produce as many values as corresponding ids, otherwise theexn:fail:contract exception is raised. A separate location is created for eachid, all of which are bound in the bodys.
Example:
(let*-values ([(id ...) val-expr] ...) body ...+)
Likelet*, except that each val-expr must produce as many values as corresponding ids. A separate location is created for each id, all of which are bound in the laterval-exprs and in the bodys.
Example:
(letrec-values ([(id ...) val-expr] ...) body ...+)
Likeletrec, except that each val-expr must produce as many values as corresponding ids. A separate location is created for each id, all of which are bound in all val-exprs and in the bodys.
Example:
(let-syntax ([id trans-expr] ...) body ...+)
Creates a transformer binding (seeTransformer Bindings) of each id with the value oftrans-expr, which is an expression at phase level 1 relative to the surrounding context. (See Identifiers, Binding, and Scopes for information on phase levels.)
The evaluation of each trans-expr is parameterized to set current-namespace to a namespace that sharesbindings and variables with the namespace being used to expand the let-syntax form, except that its base phaseis one greater.
Each id is bound in the bodys, and not in othertrans-exprs.
(letrec-syntax ([id trans-expr] ...) body ...+)
Like let-syntax, except that each id is also bound within all trans-exprs.
(let-syntaxes ([(id ...) trans-expr] ...) body ...+)
Like let-syntax, but each trans-expr must produce as many values as corresponding ids, each of which is bound to the corresponding value.
(letrec-syntaxes ([(id ...) trans-expr] ...) body ...+)
Like let-syntax, except that each id is also bound within all trans-exprs.
(letrec-syntaxes+values ([(trans-id ...) trans-expr] ...) ([(val-id ...) val-expr] ...) body ...+)
Combines letrec-syntaxes with a variant ofletrec-values: each trans-id and val-id is bound in all trans-exprs and val-exprs.
The letrec-syntaxes+values form is the core form for local compile-time bindings, since forms like letrec-syntax andinternal-definition contexts expand to it. In a fully expanded expression (see Fully Expanded Programs), the trans-idbindings are discarded and the form reduces to a combination ofletrec-values or let-values.
For variables bound by letrec-syntaxes+values, thelocation-creation rules differ slightly fromletrec-values. The [(val-id ...) val-expr] binding clauses are partitioned into minimal sets of clauses that satisfy the following rule: if a clause has a val-id binding that is referenced (in a full expansion) by the val-expr of an earlier clause, the two clauses and all in between are in the same set. If a set consists of a single clause whose val-expr does not refer to any of the clause’s val-ids, thenlocations for the val-ids are created after theval-expr is evaluated. Otherwise, locations for allval-ids in a set are created just before the firstval-expr in the set is evaluated. For the purposes of forming sets, a (quote-syntax datum #:local) form counts as a reference to all bindings in the letrec-syntaxes+valuesform
The end result of the location-creation rules is that scoping and evaluation order are the same as for letrec-values, but the compiler has more freedom to optimize away locationcreation. The rules also correspond to a nesting oflet-values and letrec-values, which is howletrec-syntaxes+values for a fully-expanded expression.
See also local, which supports local bindings withdefine, define-syntax, and more.