Debugging Lisp Function. Any Help out There?

Discussion of other Lisp dialects (Arc, Clojure, AutoLisp, XLISP, etc.)
gugamilare
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil
Contact:

Re: Debugging Lisp Function. Any Help out There?

Post by gugamilare » Thu Jun 17, 2010 6:23 am

There are lots of errors with your code.
  • That is not the way you should use cond. This is wrong:

    Code: Select all

    (cond
      ((test1 a b)
       (function1 a b))
      (cond ((test2 a b)
             (and (function2-1 a b) (function2-2 a b)))))
    This is how you do it:

    Code: Select all

    (cond
      ((test1 a b)
       (function1 a b))
      ((test2 a b)
       (function2-1 a b) (function2-2 a b)))
    You are more or less using cond like you would use if. Pay attention, cond is a shortcut to a series of if's. Not to mention you are using and to evaluate a series of forms. In cond, you don't need that. In places where you need to evaluate various forms, you should use progn.
  • There is no function or macro named setvar. I think you meant setf?. In this case, instead of

    Code: Select all

    (setf pattern1 (cdr pattern1))
    (find-post pattern1 pattern2)
    it is simpler if you just do

    Code: Select all

    (find-post (cdr pattern1) pattern2)
    Remember what I said:
    gugamilare wrote:One hint, Power User, don't use setq or setf. You don't need those, not for this function, not for most simple functions. Your code will look a lot cleaner. I'm saying this because 99% of newbies' mistakes come from misusing setq or setf.
  • Note that there is no variable named pattern2 in find-post! The function find-post does not have access to the variable pattern2 of the function match.
  • There is also a flaw in the algorithm itself when you try to (setf pattern2 (cdr y)). Even if find-post was able to see the variable pattern2 from match, the variable pattern2 would not be used anymore because match returns immediately after find-post returns.
  • Be sure to indent your code appropriately and use the tags

    Code: Select all

    [code]*your code here*
    [/code]
    to post your indented code.
This is your code partially corrected:

Code: Select all

;; Referenced lisp source code from Luger 2nd Edition, "Artificial Intelligence 4th Edition"
;; lisp pattern matcher code.
;; implement the * wild card.  (..... * .....)



(defun variable-q (x) (equal x '?))

(defun variable-a (x) (equal x '*))

(defun match-atom (pattern1 pattern2)
  (or (equal pattern1 pattern2)
      (variable-q pattern1)
      (variable-q pattern2)))

(defun find-post (x y)
  (cond
   (((not (eq x (car y))))
    (find-post x (cdr y)))
   ((eq x (car y))
    ;; No idea what this is supposed to do:
    ;; (setf pattern2 (cdr y))
    )))


(defun match (pattern1 pattern2)
  (cond
   ((or (atom pattern1) (atom pattern1))
    (match-atom pattern1 pattern2))
   ((and (listp pattern1) (variable-a (car pattern1)))
    (find-post (cdr pattern1) pattern2))
   ((and (listp pattern2) (variable-a (car pattern2)))
    (find-post (cdr pattern2) pattern1))
   (t 
    (match (car pattern1) (car pattern2))
    (match (cdr pattern1) (cdr pattern2)))))


;; eg. 
;;(match '(likes bill wine) '(likes bill wine)) T
;;(match '(likes bill wine) '(likes bill beer)) nil
;;(match '(likes ? wine) '(likes bill wine)) T
;;(match '(likes ? wine) '(likes anything)) nil
;;(match '(likes *) '(likes bill wine)) T
;;(match '(likes *) '(dislikes bill lemon-lime-bitters)) nil

(cond
  ((test1 a b)
   (function1 a b))
  ((test2 a b)
   (function2-1 a b) (function2-2 a b)))

Post Reply