Equality vs. Matching

match can be considered to be a special kind of equal:


(defun match (pat inp) (matchb pat inp '((t . t))))

; returns bindings; nil -->  match fails
(defun matchb (pat inp bindings)
  (and bindings
    (if (consp pat)
        (and (consp inp)
             (matchb (cdr pat) 
                     (cdr inp)
                     (matchb (car pat)
                             (car inp) bindings)))
        (if (varp pat)
            (let ((binding (assoc pat bindings)))
              (if binding
                  (and (equal inp (cdr binding))
                       bindings)
                  (cons (cons pat inp) bindings)))
            (and (eql pat inp) bindings)) ) ) )


(defun varp (v)     ; Test for vars, e.g. ?x
  (and (symbolp v)
       (char= #\? (char (symbol-name v) 0))) )

Contents    Page-10    Prev    Next    Page+10    Index