Contents    Page-10    Prev    Next    Page+10    Index   

Pattern Matching

Pattern matching is the inverse of substitution: it tests to see whether an input is an instance of a pattern, and if so, how it matches. [The pattern matcher code can be loaded using (load "/projects/cs375/patmatch.lsp") .]


(match '(defun ?fun (tree)
          (if (consp tree)
              (?combine (?fun (car tree))
                        (?fun (cdr tree)))
              (if (?test tree) ?trueval ?falseval)))
       '(DEFUN NNUMS (TREE)
          (IF (CONSP TREE)
              (+ (NNUMS (CAR TREE))
                 (NNUMS (CDR TREE)))
              (IF (NUMBERP TREE) 1 0))) )

((?FALSEVAL . 0) (?TRUEVAL . 1) (?TEST . NUMBERP)
 (?COMBINE . +) (?FUN . NNUMS) (T . T))

(match '(- ?x (- ?y))
       '(- z (- (* u v))))

((?Y * U V) (?X . Z) (T . T))