► |  | The Racket Reference

1 | Language Model
2 | Syntactic Forms
3 | Datatypes
4 | Structures
5 | Classes and Objects
6 | Units
7 | Contracts
8 | Pattern Matching
9 | Control Flow
10 | Concurrency and Parallelism
11 | Macros
12 | Input and Output
13 | Reflection and Security
14 | Operating System
15 | Memory Management
16 | Unsafe Operations
17 | Running Racket
| Bibliography
| Index

▼ | 2 | Syntactic Forms

2.1 | Modules: module, module*, ...
2.2 | Importing and Exporting: require and provide
2.3 | Literals: quote and #%datum
2.4 | Expression Wrapper: #%expression
2.5 | Variable References and #%top
2.6 | Locations: #%variable-reference
2.7 | Procedure Applications and #%app
2.8 | Procedure Expressions: lambda and case-lambda
2.9 | Local Binding: let, let*, letrec, ...
2.10 | Local Definitions: local
2.11 | Constructing Graphs: shared
2.12 | Conditionals: if, cond, and, and or
2.13 | Dispatch: case
2.14 | Definitions: define, define-syntax, ...
2.15 | Sequencing: begin, begin0, and begin-for-syntax
2.16 | Guarded Evaluation: when and unless
2.17 | Assignment: set! and set!-values
2.18 | Iterations and Comprehensions: for, for/ list, ...
2.19 | Continuation Marks: with-continuation-mark
2.20 | Quasiquoting: quasiquote, unquote, and unquote-splicing
2.21 | Syntax Quoting: quote-syntax
2.22 | Interaction Wrapper: #%top-interaction
2.23 | Limiting Scope: define-package, open-package, ...
2.24 | Blocks: block
2.25 | Internal-Definition Limiting: #%stratified-body
2.26 | Performance Hints: begin-encourage-inline

On this page:

begin
begin0
begin-for-syntax

top← prev up next →

#### 2.15`` Sequencing: begin, begin0, and begin-for-syntax

> > > Sequencing in The Racket Guide introduces begin and begin0.

> syntax (begin form ...)
> (begin expr ...+)

The first form applies when begin appears at the top level, at module level, or in an internal-definition position (before any expression in the internal-definition sequence). In that case, the begin 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 last expr is in tail position with respect to the begin form.

Examples:
> > (begin
> (define x 10)
> x)
> 10
> > (+ 1 (begin
> (printf "hi\n")
> 2))
> hi
> 3
> > (let-values ([(x y) (begin
> (values 1 2 3)
> (values 1 2))])
> (list x y))
> '(1 2)

> syntax (begin0 expr body ...+)

Evaluates the expr, then evaluates the bodys, ignoring the body results. The results of the expr are the results of the begin0 form, but the expr is in tail position only if no bodys are present.

Example:
> > (begin0
> (values 1 2)
> (printf "hi\n"))
> hi
> 1
> 2

> syntax (begin-for-syntax form ...)

Allowed only in a top-level context or module context, shifts the phase level of each form by one:

- expressions reference bindings at a phase level one greater than in the context of the begin-for-syntax form;

- define, define-values, define-syntax, and define-syntaxes forms bind at a phase level one greater than in the context of the begin-for-syntax form;

- in require and provide forms, the default phase level is greater, which is roughly like wrapping the content of the require form with for-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 within begin-for-syntax is parameterized to set current-namespace as in let-syntax.

top← prev up next →