Node: Assignments, Next: , Previous: Procedures, Up: Expressions



Assignments

set! <variable> <expression> R5RS
set! <proc> <arg> ... <expression> R5RS
The fisrt form of set! is the R5RS one. The second corrspond to the form defined in SRFI-17. <Expression> is evaluated, and the resulting value is stored in the location to which <variable> is bound. <Variable> must be bound either in some region enclosing the set! expression or at top level.
          (define x 2)
          (+ x 1)                   =>  3
          (set! x 4)                =>  unspecified
          (+ x 1)                   =>  5
          

The second form of set! is defined in SRFI-17. This special form set! is extended so the first operand can be a procedure application, and not just a variable. The procedure is typically one that extracts a component from some data structure. Informally, when the procedure is called in the first operand of set!, it causes the corresponding component to be replaced by the second operand. For example,

          (set (vector-ref x i) v)
          
would be equivalent to:
          (vector-set! x i v)
          

Each procedure that may be used as the first operand to set! must have a corresponding "setter" procedure. The procedure setter (see below) takes a procedure and returns the corresponding setter procedure. So,

          (set! (proc arg ...) value)
          
is equivalent to the call
          ((setter proc) arg ... value)
          

The result of the set! expression is unspecified.

setter proc R5RS
Returns the setter associated to a proc. Setters are defined in the SRFI-17 document. A setter proc, can be used in a generalized assignment, as described in set!. To associate s to the procedure p, use the following form:
          (set! (setter p) s)
          
For instance, we can write
          (set! (setter car) set-car!)
          

The following standard procedures have pre-defined setters:

          (set! (car x) v) == (set-car! x v)
          (set! (cdr x) v) == (set-cdr! x v)
          (set! (string-ref x i) v) == (string-set! x i v)
          (set! (vector-ref x i) v) == (vector-set! x i v)!
          (set! (slot-ref x 'name) v)  == (slot-set! x 'name v)
          (set! (struct-ref x 'name) v) == (struct-set! x 'name v)