next up previous contents
Next: Using matches to Up: No Title Previous: Exercises on the


We have already seen how the equality symbol "=" can be used to compare two lists. The operation MATCHES provides more sophisticated facilities for matching lists against a partially specified pattern.

(In later versions of Pop-11 the equality operator "=" will be generalised to include this matching capability, based on a new kind of matchvar data-type).

The pattern matcher makes it possible to express some complex ideas in a way that is both much clearer and easier to read than normal procedural formulations, and also easier to get right first time when programs are being developed. So for many kinds of programs it can substantially speed up the development time. There are additional facilities based on the pattern matcher in the Pop-11 database, and these add to the power of the language for solving quite complex problems, including developing expert systems.

The matcher can be used in several different formats. The most common is an expression of this form, which always evaluates to TRUE or FALSE, depending whether the list does or does not match the pattern.

    <list> matches <pattern>

;;; Simple examples, using the matcher like an equality tester:

    [a b c d] matches [a b c d] =>
    ** <true>

    [a b c d] matches [d b c a] =>
    ** <false>
The use of pattern elements allows the matcher to perform more complex comparisons. The pattern matcher assumes that information is stored in lists whose contents can be searched by specifying a pattern (a sort of template list) containing special pattern elements described below. Some of these pattern elements are used to match individual items in a list. Others can match arbitrarily long subsets of lists and are called "segment" pattern elements. We illustrate the power of segment pattern elements first.

The anonymous segment pattern element: "=="

We start by illustrating the use of the segment pattern element "==" to create templates that can be used to match lists.

The symbol "==", which we have previously met as an equality symbol can be used in patterns with an entirely different function, to represent an unspecified number of unknown elements. For example both the following complex expressions denote TRUE

    [who is the father of joe] matches [who is == ]

    [where is the father of joe] matches [where is == ]
The following also denote TRUE:

    [you are my favourite programming pupil] matches [== pupil]

    [the little dog laughed to see such fun] matches [== dog ==]

next up previous contents
Next: Using matches to Up: No Title Previous: Exercises on the

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