next up previous contents
Next: updatersthe stack Up: CHAPTER.3: PROCEDURES AND Previous: Other updaters: subscrv

Using a numerical subscript to access or update a structure

In Pop-11, it is possible to access or update the N'th element of a list or vector type datastructures simply by applying the structure to the number N.

    vars
        list1   = [a list of words],
        vec1    = {contents of vector},
        string1 = 'ABCDE';
In each case we can access the second element simply by applying the structure to the number 2:

    list1(2) =>
    ** list

    vec1(2) =>
    ** of

    string1(2) =>
    ** 66
Similarly a numerical subscript can be used for updating:

    "set" -> list1(2);
    list1 =>
    ** [a set of words]

    "without" -> vec1(2);
    vec1 =>
    ** {contents without vector}

    `?` -> string1(2);
    string1 =>
    ** A?CDE
This facility makes it easy to write a program that will operate on objects of different types, e.g.:

    define iselement(item, structure) -> answer;
        lvars num;
        for num from 1 to length(structure) do
            if item == structure(num) then
                true -> answer;
                return();
            endif;
        endfor;

        false -> answer;
    enddefine;

    iselement("set", list1) =>
    ** <true>

    iselement("set", vec1) =>
    ** <false>

    iselement(`?`, string1) =>
    ** <true>


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