next up previous contents
Next: Retrieving details of Up: CHAPTER.7: THE POP-11 Previous: Using variables in

Matching a "segment" of a list

Had the value of cupboard been

    ** [shoes tins brushes blanket pillow]
then that MATCH would have failed.

We need to `strip off' the list brackets of the value of BOX if we are to match this new kind of CUPBOARD, and we can do this by using a double up-arrow:

    [^^box blanket ==] =>
    ** [shoes tins brushes blanket ==]
This uses the value of the variable box to insert a collection of items to form a SEGMENT of the list, rather than inserting a single list as an ELEMENT of the enclosing list.

To illustrate the power of matches and the pattern language, consider the following definition of the procedure ismember which could be compared with the definition of iselement in Chapter 3:

    define ismember(item, list) -> trueorfalse;
        list matches [== ^item ==] -> trueorfalse
    enddefine;

    ismember(3, [ 1 2 4 ] ) =>
    ** <false>
    ismember(3, [ 1 2 3 4 ] ) =>
    ** <true>
    ismember("c", [ a b c d e]) =>
    ** <true>
This uses the fact that matches returns a `boolean' result, i.e. either TRUE or FALSE.



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