Next: , Previous: Definitions, Up: Special Forms


2.5 Assignments

— special form: set! variable [expression]

If expression is specified, evaluates expression and stores the resulting value in the location to which variable is bound. If expression is omitted, variable is altered to be unassigned; a subsequent reference to such a variable is an error. In either case, the value of the set! expression is unspecified.

Variable must be bound either in some region enclosing the set! expression, or at the top level. However, variable is permitted to be unassigned when the set! form is entered.

          (define x 2)                            =>  unspecified
          (+ x 1)                                 =>  3
          (set! x 4)                              =>  unspecified
          (+ x 1)                                 =>  5
     

Variable may be an access expression (see Environments). This allows you to assign variables in an arbitrary environment. For example,

          (define x (let ((y 0)) (the-environment)))
          (define y 'a)
          y                                       =>  a
          (access y x)                            =>  0
          (set! (access y x) 1)                   =>  unspecified
          y                                       =>  a
          (access y x)                            =>  1