next up previous contents
Next: Setting a value Up: CHAPTER.7: THE POP-11 Previous: The matcher arrow

Findroom revisited

We can illustrate the use of the matcher to simplify the definition of a searching procedure, by redefining the procedure `findroom' introduced in chapter 1, and defined thus:

    define findroom(name, list_of_lists) -> data;
        ;;; search list_of_lists for one starting with name

        for data in list_of_lists do
            if data(1) = name then
                return();       ;;; i.e. stop the procedure
            endif;
        endfor;

        ;;; produce a mishap message
        mishap('DATA NOT FOUND', [^name ^list_of_lists])
    enddefine;
Compare this with the following:

    define findroom(name, list) -> data;
        vars len, breadth, height;  ;;; pattern variables

        if list matches [ ==  [^name ?len ?breadth ?height] == ]
        then
            [^name ^len ^breadth ^height] -> data;
        else
            mishap('DATA NOT FOUND', [^name ^list])
        endif
    enddefine;
The line

        if list matches [ == [^name ?len ?breadth ?height] == ]
runs the operation called `matches' with two inputs, the value of the variable `list', and the pattern on the right, which instructs the matcher what to look for in the list. It says, look for any number of elements (matched against "=="), then a list starting with the given name and containing three other things, followed by any number of elements (matched against "==" again).



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