How to write this functionality

Discussion of Common Lisp
Post Reply
prathyuguduri
Posts: 12
Joined: Tue Apr 20, 2010 11:12 pm

How to write this functionality

Post by prathyuguduri » Fri Apr 23, 2010 12:17 pm

What I want to do is

for each sos (could be 1 or 2 elements like (g) or (not g))
I want to check if "g" is in the clauses(reverse kb) list.
If there is a match put it in the "pred" list and do one of the "and's"
How do we write someth like "for each sos loop..." in LISP?

This is the kb list (KB '((or c d) (or a b) (or not d b) (or not c e) (or not c a) (or not e f) (or not b f) (or not f g) (not g))).

Thanks for any help.

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: How to write this functionality

Post by nuntius » Fri Apr 23, 2010 5:41 pm

Something like this?

Code: Select all

(defun process-gs (kb)
  (let ((pred nil))
    (dolist (clause (reverse kb))
      (when (find 'g clause)
        (push clause pred)
        ;; do other stuff?
        ))
    pred))

(process-gs '((or c d) (or a b) (or not d b) (or not c e) (or not c a) (or not e f) (or not b f) (or not f g) (not g)))
-> ((OR NOT F G) (NOT G))

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: How to write this functionality

Post by nuntius » Fri Apr 23, 2010 6:00 pm

Somewhat neater,

Code: Select all

(defun process-gs (kb)
  (remove-if-not
   (lambda (clause) (find 'g clause))
   kb))
If you need to do more processing on matching clauses, you can then call MAPC on the matching list.

prathyuguduri
Posts: 12
Joined: Tue Apr 20, 2010 11:12 pm

Re: How to write this functionality

Post by prathyuguduri » Mon Apr 26, 2010 6:40 pm

@ nuntius

Thank you for the reply.

I have implemented what you told me but it is still just comparing only the next list in the list of lists to another list, and if its not there then i get an error i.e

(setq KB '((or a b) (or not c a) (or not b f) (or not d b) (or c d) (or not c e) (or not f g) (or not e f) (not g)))
The first time sos is (not g), It compares with the rest (reverse kb). so it finds a match 6 times. But if I change the order of kb, then Error: In - of (NIL 1) arguments should be of type NUMBER.

But i need to search the kb until I find a match. How to implement this?

Another question: When writing dolist could we check 2 lists, if there is no match in the first list then it should go to the second list. And also in dolist could we append to the list that we are checking?

Sorry for too many questions. Hope you can help me. Thanks.

Post Reply