next up previous contents
Next: Invoking the updater Up: CHAPTER.3: PROCEDURES AND Previous: The pretty print

Using assignment to store something in a structure

So far the assignment arrow has been used only with a variable on the right hand side.

Besides storing things in variables, assignment can be used to store something in a structure. E.g. we may create a list, then use assignment to alter its third element:

    vars list = [a b c d e];
    list =>
    ** [a b c d e]

    "D" -> list(4);         ;;; alter the 4th element
    list =>
    ** [a b c D e]
Here the assignment has "updated" the previously existing list, unlike the expression

    [a b c D e] =>
which creates a new list. This is an example of what is sometimes called programming with "side effects". I.e. when an instruction is obeyed, it not only produces results which can be assigned to variables, or used to construct new datastructures, but it also alters a pre-existing structure. Side effects are often a source of obscure bugs in programs, and there are some computer scientists who think they should not be allowed, so they prefer to use "pure functional" languages that permit no side effects. (An example of such a language is Miranda.) However, there are many ways of designing useful software that make use of side effects, so Pop-11 allows them. If you use these facilities you will need to be very careful.



Aaron Sloman
Fri Jan 2 03:17:44 GMT 1998