3.17 Assignment: set! and set!-values

► |  | 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:

set!
set!-values

top ← prev up next →

#### 3.17`` Assignment: set! and set!-values

> > > Assignment: set! in The Racket Guide introduces set!.

> syntax (set! id expr)

If id has a transformer binding to an assignment transformer, as produced by make-set!-transformer or as an instance of a structure type with the prop:set!-transformer property, then this form is expanded by calling the assignment transformer with the full expressions. If id has a transformer binding to a rename transformer as produced by make-rename-transformer or as an instance of a structure type with the prop:rename-transformer property, then this form is expanded by replacing id with the target identifier (e.g., the one provided to make-rename-transformer). If a transformer binding has both prop:set!-transformer and prop:rename-transformer properties, the latter takes precedence.

Otherwise, evaluates expr and installs the result into the location for id, which must be bound as a local variable or defined as a top-level variable or module-level variable. If id refers to an imported binding, a syntax error is reported. If id refers to a top-level variable that has not been defined, the exn:fail:contract exception is raised.

See also compile-allow-set!-undefined.

Examples:

> > (define x 12)
> > (set! x (add1 x))
> > x
> 13
> > (let ([x 5])
> (set! x (add1 x))
> x)
> 6
> > (set! i-am-not-defined 10)
> set!: assignment disallowed;
> cannot set undefined
> variable: i-am-not-defined

> syntax (set!-values (id ...) expr)

Assuming that all ids refer to variables, this form evaluates expr, which must produce as many values as supplied ids. The location of each id is filled with the corresponding value from expr in the same way as for set!.

Example:

> > (let ([a 1]
> [b 2])
> (set!-values (a b) (values b a))
> (list a b))
> '(2 1)

More generally, the set!-values form is expanded to

> (let-values ([(tmp-id ...) expr])
> (set! id tmp-id) ...)

which triggers further expansion if any id has a transformer binding to an assignment transformer.

top ← prev up next →