DEFMATCH


Syntax

 
defmatch  name  (  pattern  lambda-list  )
     {  lisp-form  }*
=> compiled-function
Description
DEFMATCH compiles a lambda expression containing the series of lisp-form as its progbody. Effectively it dfines a method which will only act on the arguments it is given if the first argument matches its pattern and remaining arguments match its lambda-list. Note: patterns in DEFMATCH calls are not quoted.

As part of the action of DEFMATCH the symbol name has its symbol-value set to a  ( pattern lambda-expression ) association and its function name set to a function which applies matching and applies the
associated lambda expression.

If a match method defined with DEFMATCH is called with an argument that fails to match any of the methods patterns the call results in a value of nil.

Match method operation can be traced using the standard Lisp trace mechanism.

Matcher methods are tested for application by attempting to match their pattern with the first argument supplied in their call. Matcher methods are tested in the order in which they are compiled, this means that if a generalised pattern is used for a method later methods with more specialised patterns will never be used.

Matcher methods can be incrementally compiled, ie: after editing an individual method can be comiled without having to compile all methods but the following resrtictions apply...

Examples
 
(defmatch silly ((the ?x ate the ?y))
  `(digested ,#?x ,#?y))

(defmatch silly ((the ?x chased the ?y))
  `(ran-after ,#?x ,#?y))

(defmatch silly ((the ?x ?act the ?y))
  (list #?act #?x #?y))

(defmatch silly (nil)
  '(a null event))
 

> (silly '(the cat ate the cheese))
==> (DIGESTED CAT CHEESE)

> (silly '(the worm chewed the apple))
==> (CHEWED WORM APPLE)

> (silly ())
==> (A NULL EVENT)


(defmatch daft ((?x plus ?y) z &key (scale 1))
  (* (+ #?x #?y z) scale))

> (daft '(5 plus 3) 7 :scale 10)
==> 150


(defmatch len1 (())
  0)

(defmatch len1 ((??x))
  (1+ (len1 (cdr #?x))))

> (len1 '(herring haddock hake))
==>3

> (trace len1)
(LEN1)
> (len1 '(herring haddock hake))
; 14> LEN1 called with arg: (HERRING HADDOCK HAKE)
; | 15> LEN1 called with arg: (HADDOCK HAKE)
; | | 16> LEN1 called with arg: (HAKE)
; | | | 17> LEN1 called with arg: NIL
; | | | 17< LEN1 returns value: 0
; | | 16< LEN1 returns value: 1
; | 15< LEN1 returns value: 2
; 14< LEN1 returns value: 3
==> 3


(defmatch palindrome ((?x ?x))
  T)

(defmatch palindrome ((?x))
  T)

(defmatch palindrome ((?x ??rest ?x))
  (palindrome #?rest))

> (palindrome '(dog frog kipper frog dog))
==> T

> (palindrome '(dog frog kipper cat frog dog))
==> NIL