Mutators

In most languages, the assignment statement is among the most important. Scheme, however, is designed to support a programming paradigm known as 'functional programming', which aims to simplfy program design by minimizing the amount of global state. This is primarily done by reducing the number of side-effects a function has - ideally, a function should have no affect on global state other than returning its value, and should always return the same value for a given set of arguments, as a mathematical function would.

While some FP languages try to enforce this ideal, Scheme does not force the programmer's hand. In Scheme, there exist a set of re-assignment functions, or mutators, which are by convention marked with an exclamation point to indicate that they actually change the value of one or more of their arguments. The most important mutator is (set!) which replaces the value of its first argument with that of its second argument:

   > (define a 1)
   > a
   1
   > (set! a 2)
   > a
   2
  

Note that (set!) can only change a variable that has already been initialized with (define). There also two list mutators, (set-car!) and (set-cdr!), which, predictably, change only the CAR or the CDR of the modified variable.

Use of these mutators is rare in Scheme, and is mostly used for changing global tables and so forth. In general, it is possible to avoid them in nearly 90% of all Scheme code.

Contents Previous Next