Patterns


A pattern is a generalised list (containing symbols, sublists and possibly objects of other types) which usually also contains tags and/or wild cards (see below). Pattern lists are matched against data lists (which may also contain symbols, sublists and other objects). The process of matching either succeeds (matching is possible) or fails (matching is not possible), in the event of a successful match there may be side effects which include binding values to matcher variables and pseudo Lisp variables (see examples).
 

tags & wildcards

The matcher functions use four tags/wildcards in patters (with the exception of MATCH-BIND wich uses only two). These are as follows...
 
 
tag/wildcard
description
= match a single item, typically an atom or a sublist
== match zero or more items
?var match a single item and bind it to a match variable named var
??var match zero or more items and bind them to a match variable named var

Notes...

The first (left-most) use forms like ?var  and ??var in a pattern will cause the creation of a matcher variable and an equivalent Lisp variable. Other uses of that variable in a pattern or in nested calles to matcher functions will cause a failed match unless bindings are consistent. See examples below.
Matcher bindings can be retrieved using MATCH-BIND or using equivalent Lisp variables.


Examples of using tags with MLET...
 

> (mlet ('(the ?subj ate the ?obj) '(the mouse ate the cheese))
    (list #?subj #?obj))
==> (MOUSE CHEESE)
 

> (mlet ('(= ?2nd == ?last) '(a b c d e f g))
    (list #?2nd #?last))
==> (B G)
 

> (mlet ('(?det ??act ?det ??obj)
         '(the cat sat on the red mat))
     (list #?act #?obj))
==> ((cat sat on) (red mat))
 

> (mlet ('(?first ==) '(a b c))
     (mlet ('(== ?first ??rest) '(t r a v e l l e r))
        #?rest))
==> (v e l l e r)