ARE YOU?) 4. Write a function (defun find -match (pred facts) .. .) that finds t
ID: 3745053 • Letter: A
Question
ARE YOU?) 4. Write a function (defun find -match (pred facts) .. .) that finds the first match of pred (e.g., (on ?x ?y)) to an element of the facts st (e.g., ((on-table B1) (on-table B2) (on B3 B2) (on B4 B3 ))). For these example inputs the result would be the variables of pred with their values determined by the match, ((?X B3) (?Y B2)). Note that the predicate itself, here 'on', is assumed to be a symbol (not a number or string, etc.), and variables are symbols starting with '?' . However, the values of arguments among the 'facts' could be arbitrary s-expressions such as in the predication (touching (right-side-of B1) (eft-side-of B2)). predicates might have zero arguments, e.g. , (rainy-weather). In principle, A match in such a case should just give T. Failure to find a match should give NILExplanation / Answer
(defun find-match (pred facts)
(loop for item in facts do
;; Going through all the items in the facts list
(if (and (= (length pred) (length item)) (equal (car pred) (car item)))
;; Finding a match checking if the length of the predicate is equal to
;; that of the item AND if the first element (the predicate) matches with
;; first element of the fact
(if (= (length pred) 1)
;; This is to check for predicates with zero arguments, incase it exists
;; it will return T to show that it does, else return NIL
(return-from find-match T)
(return-from find-match (mapcar #'list (cdr pred) (cdr item)))
;; Uses mapcar to make a list of the values determined by their matches
))))