next up previous contents
Next: Findroom revisited Up: CHAPTER.7: THE POP-11 Previous: The use of

The matcher arrow "-->"

The matcher arrow is often very useful when you are sure two things will match, but you want to use the matcher to examine the contents of one of them. That way you don't use a conditional expression of the form:

    if ... matches ... then ....
Instead we use the matcher arrow "-->". "-->" should not be confused with "->". The latter is the assignment arrow.

The infix operation "-->" could have been defined thus:

    define 8 datum --> pattern;
        unless datum matches pattern then
            mishap('NON MATCHING ARGUMENTS FOR -->',[%datum,pattern%])
        endunless
    enddefine;
The `8' in the procedure heading indicates that an infix operation of precedence 8 is being defined.

This definition looks as if it doesn't do anything when the datum matches the pattern. We shall see that, on the contrary, it can be used to `decompose' a list.

Examples of the use of "-->"

The simplest use of `-->' is to check that a list has a certain format:

    list --> [junction == ];
checks that LIST starts with "JUNCTION". If not, an error occurs.

        [a b c d] --> [junction ==];

        ;;; MISHAP - NON MATCHING ARGUMENTS FOR -->
        ;;; INVOLVING:  [a b c d] [junction ==]
        ;;; DOING    :  --> compile
The operation --> can also be used to decompose a list, i.e. to assign some of its components to variables.

    vars first, second, rest;
    vars list = [dogs like wagging their tails];

    list --> [?first ?second ??rest];
    first =>
    ** dogs
    second =>
    ** like
    rest =>
    ** [wagging their tails]
That would have caused an error if LIST had had fewer than two elements. Since there are at least two elements, it gives FIRST the first element, as its value, SECOND the second element, and REST a list containing all the remainder.

Here's another example.

    vars first, second, rest;

    [mary had a little lamb] --> [?first ?second ??rest];

    first =>
    ** mary

    second =>
    ** had

    rest =>
    ** [a little lamb]
Notice the different effects of "?" and "??". The former means, roughly "match ONE element", the latter means, roughly "match any number of elements". The use of "??" and "==" can mean that there are alternative ways of matching a list againt a pattern. In that case the matcher will find only one of them. Which one it finds is not defined. The Birmingham ftp directory has a library called "doesmatch" that finds all the possible matches.



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