3.17 Assignment: set! and set!-values
Assignment: set! in The Racket Guide introduces set!.
syntax
(set! id expr)
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.
> (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 variable before its definition
variable: i-am-not-defined
in module: top-level
syntax
(set!-values (id ...) expr)
> (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.