Reference: PLT Scheme

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
11`` | Macros
12`` | Input and Output
13`` | Reflection and Security
14`` | Operating System
15`` | Memory Management
16`` | Running PLT Scheme
| Bibliography
| Index

On this page:

set!
set!-values

Version: 4.0.2
contents index
← prev up next →

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

Assignment: set! in Guide: PLT Scheme introduces set!.

(set! id expr)

If id has a transformer binding to an assignment transformer, as produced by make-set!-transformer, 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, then this form is expanded by replacing id with the one provided to make-rename-transformer.

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!: cannot set undefined identifier: i-am-not-defined

(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 wih to the corresponding value from expr in the same way as for set!.

Examples:

> (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.

contents index
← prev up next →