next up previous contents
Next: Arrays are procedures Up: CHAPTER.4: PROCEDURES IN Previous: Exercises on procedure

Using non-procedures (e.g. lists, vectors) as procedures

So far we have seen how procedures can be used as data, e.g. giving double as argument to the composition operator, and saving the created procedure in a variable, etc.

Not only can many different things be done with procedures in Pop-11: it is also possible to treat certain objects as if they were procedures in order to obtain general and modular programs. For example, consider a list of three words:

    vars numlist = [cat dog mouse];
Although this is not actually a procedure, it has, from the mathematical point of view, some of the properties of a procedure (or a function) in that you can think of it as mapping the first three integers onto three words. We can define a procedure that does this:

    define int_to_word(int) -> result;
        if int == 1 then "cat" -> result;
        elseif int == 2 then "dog" -> result
        elseif int == 3 then "mouse" -> result
        else mishap('NUMBER TOO BIG', [^int])
then we can use the procedure thus:

    int_to_word(1) =>
    ** cat
    int_to_word(2) =>
    ** dog
    int_to_word(3) =>
    ** mouse
    int_to_word(4) =>
    ;;; INVOLVING:  4
    ;;; DOING    :  int_to_word compile ...
However, in Pop-11 you do not need to define such a procedure. You can use the list itself as a procedure by applying it to integers as if it were a procedure, or mathematical function, thus:

    numlist(1) =>
    ** cat
    numlist(3) =>
    ** mouse
    numlist(4) =>
    ;;; INVOLVING:  4 [cat dog mouse]
    ;;; DOING    :  ...
Moreover, because numlist is a list, its contents can be changed, altering the behaviour of the procedure or function that it simulates:

    "snake" -> numlist(2);

    numlist(2) =>
    ** snake
Similarly vectors and strings can be treated as procedures, in Pop-11.

        vec = {966 77 88 99},
        string = 'the cat';

    vec(3) =>
    ** 88

    string(1) =>
    ** 116

    string(2) == `h` =>
    ** <true>
(Remember that the strings are actually vectors of 8 bit integers, representing character codes, as described in HELP ASCII, HELP STRINGS)

(NOTE: This use of integers for indexed access does not work with records in Pop-11: they are assumed to have components that are accessible only via particular named procedures associated with the record class.)

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