► |  | The Racket Reference

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

▼ | 3 | Syntactic Forms

3.1 | Modules: module, module*, ...
3.2 | Importing and Exporting: require and provide
3.3 | Literals: quote and #%datum
3.4 | Expression Wrapper: #%expression
3.5 | Variable References and #%top
3.6 | Locations: #%variable-reference
3.7 | Procedure Applications and #%app
3.8 | Procedure Expressions: lambda and case-lambda
3.9 | Local Binding: let, let*, letrec, ...
3.10 | Local Definitions: local
3.11 | Constructing Graphs: shared
3.12 | Conditionals: if, cond, and, and or
3.13 | Dispatch: case
3.14 | Definitions: define, define-syntax, ...
3.15 | Sequencing: begin, begin0, and begin-for-syntax
3.16 | Guarded Evaluation: when and unless
3.17 | Assignment: set! and set!-values
3.18 | Iterations and Comprehensions: for, for/ list, ...
3.19 | Continuation Marks: with-continuation-mark
3.20 | Quasiquoting: quasiquote, unquote, and unquote-splicing
3.21 | Syntax Quoting: quote-syntax
3.22 | Interaction Wrapper: #%top-interaction
3.23 | Blocks: block
3.24 | Internal-Definition Limiting: #%stratified-body
3.25 | Performance Hints: begin-encourage-inline
3.26 | Importing Modules Lazily: lazy-require

On this page:

begin
begin0
begin-for-syntax

top ← prev up next →

#### 3.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 ...+)

Evaluates the first expr, then evaluates the other exprss in order, ignoring their results. The results of the first expr are the results of the begin0 form; the first expr is in tail position only if no other exprs 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 →