3.15 Sequencing: begin, begin0, and begin-for-syntax (original) (raw)
3.15 Sequencing: begin, begin0, and begin-for-syntax🔗ℹ
Sequencing in The Racket Guide introduces begin and begin0.
(begin form ...) (begin expr ...+)
The first form applies when begin appears at the top level, at module level, or in an internal-definition position. In that case, thebegin form is equivalent to splicing the forms into the enclosing context.
The second form applies for begin in an expression position. In that case, the exprs are evaluated in order, and the results are ignored for all but the last expr. The lastexpr is in tail position with respect to the beginform.
Examples:
Evaluates the first expr, then evaluates the other exprss in order, ignoring their results. The results of the first exprare the results of the begin0 form; the first expr is in tail position only if no other exprs are present.
Example:
(begin-for-syntax form ...)
- expressions reference bindings at a phase level one greater than in the context of the begin-for-syntaxform;
- define, define-values,define-syntax, and define-syntaxes forms bind at a phase level one greater than in the context of thebegin-for-syntax form;
- in require and provide forms, the defaultphase level is greater, which is roughly like wrapping the content of the require form withfor-syntax;
- expression form expr: converted to(define-values-for-syntax () (begin expr (values))), which effectively evaluates the expression at expansion time and, in the case of a module context, preserves the expression for future visits of the module.
See also module for information about expansion order and partial expansion for begin-for-syntax within a module context. Evaluation of an expr withinbegin-for-syntax is parameterized to setcurrent-namespace as in let-syntax.