Changing Values: set!

The special form set! can be used to change the value of a variable (either a local variable or a global variable).

      (set! name newvalue )

The name is not evaluated, but its binding is changed so that its value becomes newvalue. set! is analogous to the assignment operator = or := in other languages. In Common Lisp, setq is used instead of set!.

   (set! sum (+ sum x))

Changing the value of a variable is a side-effect (an effect of a computation other than returning a value). The ! emphasizes that set! has a side-effect. Scheme purists avoid the use of set!, although assignment is a very common operation in most programming languages. Functional programming uses only the function call mechanism and does not use set! at all; it is possible to write any program in a purely functional style.

Contents    Page-10    Prev    Next    Page+10    Index