next up previous contents
Next: Some procedures which Up: CHAPTER.3: PROCEDURES AND Previous: Exercise

Multiple assignments

All the following imperatives use the stack:

    (a) expression =>
    (b) expression;
    (c) expression -> variable;
    (d) -> variable;
we can replace `expression' by a sequence of expressions separated by commas and we can replace `-> variable' by a sequence of such things, for example:

    1, 2, 3 -> z -> y -> x;
After this imperative has been executed z will have the value 3 and x will have the value 1. Because the assignments remove the items from the stack in the reverse order from their placement on the stack, this notation can be confusion. So Pop-11 allows a special syntax for assigning to several variables in the natural order. The above instruction is equivalent to:

    1, 2, 3 -> (x, y, z);
    x =>
    ** 1
More usefully, after executing:

    x, y -> x -> y;
or, expressing the same thing more clearly:

    x, y -> (y, x);
the values of x and y will have been swapped.

Beware of writing programs which leave things lying around on the stack for later use in a wanton fashion. Some people do this for the sake of efficiency, since stack operations are very fast. However it can make programs hard to understand, debug, or modify.



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