In order to follow what happened in the example, care has to be taken in interpreting the pattern matching variables used in the various list patterns. As this is quite an intricate feature of POP-11 programming, it would be as well to go over the most important features of the use of variables in list patterns. So this section expands on the discussions of pattern matching in the last chapter. Reading this section is not essential if you are not at present concerned with gaining a practical knowledge of POP-11 programming through reading this book.
Two things have to be kept clear in understanding variables in list patterns.
First, there is the difference between set-value and use-value variables. Set-value variables look like this:
?x ??y ?item ??items
and use-value variables look like this:
^x ^^y ^item ^^items
A set-value variable is used when a value is being looked for to assign to that variable. A use-value variable is used when the variable has already had a value assigned to it, and where that value is to be used. As you can see, set-value variables are always preceded by queries and use-value variables are always preceded by up-arrows.
To illustrate: take, for example, the pattern
[?y near ^x]
Here the variable y is to be given a value, while the variable x will already have a value assigned to it, and that value will be used in place of x in the pattern.
Suppose, for example, that the value
[hyde park]
has been assigned to the variable
x,
and the pattern
[?y near ^
x]
is being used to search the database. There will then be a match with the
following database entry:
[[marble arch] near [hyde park]]
and, as a result, the variable y will be set to the list [marble arch].
Related to set-value variables, are wild cards, with single or double equals-signs:
= ==
As we explained in chapter 2, these stand for single or multiple items in a list, and are used where one does not care what items those are (so one does not need to store them).
The second point that needs to be kept clear in the use of list-patterns is the difference between single and double queries, up-arrows, or equals-signs preceding variables in patterns. This is the difference between match-one pattern variables like
?x ?item ^y ^item =
and match-any pattern variables such as
??x ??items ^^y ^^items ==
The single pattern-variables are used to match against one and only one item in a list (that item may itself be a list). The double pattern-variables are used to match against any number of items in a list: that is, zero or more.
There are a few things to note about these variables. In the case of double query pattern-variables, the items which are picked up are always enclosed in list brackets. So when, for instance, the pattern
[direct me to ??x]
is matched with
[direct me to great portland street]
the variable x will acquire the value
[great portland street]
Again, if it were matched with
[direct me to piccadilly]
then x would acquire the value
[piccadilly]
and not the word piccadilly. Notice that if it were matched with
[direct me to]
then x would acquire the value [ ].
Conversely, when double up-arrows are used, the contents of a list are to be inserted into a pattern, and the rule here is `insert the items in the list without the outer list brackets'. So the pattern
[direct me to ^^
x]
where x has the value
[great portland street]
will result in the list
[direct me to great portland street]
being created. By contrast, if we had used the pattern
[direct me to ^
x]
then the result would be
[direct me to [great portland street]]
Having gone over these points, go back through the answer procedure given above, trying to see how the various pattern-variables operate there.