Contents    Page-10    Prev    Next    Page+10    Index   

Match Function


(defn equal [x y]
  (if (cons? x)
      (and (cons? y)
           (equal (first x) (first y))
           (equal (rest x) (rest y)))
      (= x y) ))

(defn matchb [pat inp bindings]
(if (cons? bindings)   ; if not, already failed
    (if (cons? pat)        ; if pat is a cons
        (and (cons? inp)   ; inp must be a cons
             (matchb (rest pat)  ; parts must match
                     (rest inp)
                     (matchb (first pat)
                             (first inp) bindings)))
        (if (varp pat)     ; not a cons: a var?
            (if (assocl pat bindings)
                (and (equal inp  ; existing binding
                      (second (assocl pat bindings)))
                     bindings)
                (cons (list pat inp) bindings))
            (and (= pat inp) bindings)))) )

(defn match [pat inp]  (matchb pat inp '((t t))))