match pattern
Posted: Tue Oct 30, 2012 8:14 pm
I am having problems with this function. What I have worked so far is that it checks for variable that are denoted by "?" For instance variable x is denoted by (? x). so what Im looking to do with it is given two list for instance:
ïƒ (match '(A (? X) (? Y)) '(A (E F) G))
(T ((X (E F)) (Y G)))
ïƒ (match '(A (? X) (? X)) '(A (E F) G))
(NIL ((X (E F))))
ïƒ (match '(A (? X) (? Y (numberp Y))) '(A (E F) 7))
(T ((X (E F)) (Y 7)))
ïƒ (match '(A (? X) (? Y (numberp Y))) '(A (E F) G))
(NIL ((X (E F))))
ïƒ (match '(A (? X) (? Y (numberp Y) (> Y 10))) '(A (E F) 15))
(T ((X (E F)) (Y 15)))
ïƒ (match '(A (? X) (? Y (numberp Y) (my-predicate Y 10))) '(A (E F) 7))
(NIL ((X (E F))))
So far my code works half the way..i think. could anyone help me...to get this right. I feel like i go over and over and do no improvement.
ïƒ (match '(A (? X) (? Y)) '(A (E F) G))
(T ((X (E F)) (Y G)))
ïƒ (match '(A (? X) (? X)) '(A (E F) G))
(NIL ((X (E F))))
ïƒ (match '(A (? X) (? Y (numberp Y))) '(A (E F) 7))
(T ((X (E F)) (Y 7)))
ïƒ (match '(A (? X) (? Y (numberp Y))) '(A (E F) G))
(NIL ((X (E F))))
ïƒ (match '(A (? X) (? Y (numberp Y) (> Y 10))) '(A (E F) 15))
(T ((X (E F)) (Y 15)))
ïƒ (match '(A (? X) (? Y (numberp Y) (my-predicate Y 10))) '(A (E F) 7))
(NIL ((X (E F))))
So far my code works half the way..i think. could anyone help me...to get this right. I feel like i go over and over and do no improvement.
Code: Select all
(defun match (p s &optional (binds () ))
(let ( (temp nil) )
(cond ((atom p) (cond ((equal p s) (list t binds))
(t (list nil nil))))
((equal (first p) '?) (setf temp (test-binds (second p) s binds)) (cond (temp (list t p))
(t (list nil nil))
)
)
((atom s) (list nil nil))
(t (setf temp (match (first p) (first s) binds))
; temp = (flag binds)
(cond ((first temp) (match (rest p) (rest s) (second temp)))
; (first p) and (first s) match
(t (list nil nil))
)
)
)
)
)
Code: Select all
(defun test-binds (x v binds)
;; returns nil or the binds updated by the addition of the pair (x v)
(let ( (y nil) )
(setf y (assoc x binds))
(cond
(y (cond ((equal (second y) v) binds)(t nil)))
(t (setf binds (append binds (list (list x v)))))
)
)
)