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

Use of the matcher to extract the contents of a list

In the previous uses of the matcher with "==" the portion of the list that corresponded to "==" was ignored. We might want to revise the above procedure so that it extracted that portion and returned it as a result. For that we use a "segment variable" pattern element, in which a variable name is preceded by "??". The use of "??" indicates that the variable should match an arbitrary number of items in the datum, and if the whole match is successful a list containing those items should be assigned to the variable. Here is an example, in which we change the above procedure to return two results, the type of question, and the contents of the question. Note that we have to use "vars" to declare a variable as a pattern variable:

    define type_of_answer(list) -> (type, contents);

        vars remainder;

        if     list matches [who is ??remainder] then
            "person" -> type;
            remainder -> contents;
        elseif list matches [where is ??remainder ] then
            "place" -> type;
            remainder -> contents;
            "undef" -> type;
            [] -> contents;
This can now be tested:

    type_of_answer([why can pigs fly]) =>
    ** undef []

    type_of_answer([who is the murderer]) =>
    ** person [the murderer]

    type_of_answer([where is the murderer]) =>
    ** place [the murderer]

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