Page 1 of 1

can we search 2 lists at a time?

Posted: Tue Apr 27, 2010 12:29 am
by prathyuguduri
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?

(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?

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

Re: can we search 2 lists at a time?

Posted: Tue Apr 27, 2010 7:38 am
by Jasper
Did you see this? I have no idea if that is near your question, but those NOTs and ORs seem to imply it.

It is better to refer not to the variables, but to what they are/mean? Or in terms of a function having certain inputs, and what it is to output? I dubbed KB to be called clause; a combination of (OR ..) true if test on one succeed,(returning first one) (AND ...) true only if test on all succeeds, (NOT ...) true if none of the tests succeed. But i am not sure this is what you want.

Re: can we search 2 lists at a time?

Posted: Tue Apr 27, 2010 7:47 am
by nuntius
prathyuguduri, could you post the code you have developed? Both Jasper and I posted solutions to what we think you want.

As for checking two lists, you can either check them one at a time; or check (append list1 list2).

Re: can we search 2 lists at a time?

Posted: Tue Apr 27, 2010 10:24 am
by prathyuguduri

Code: Select all

(defun process (kb)
(let* ((sos (first (last KB)))
         (soslist (list sos))
		 (clauses (rest (reverse kb)))
         (pred1 (first (last (butlast kb))))
		 (notpos (- (position (second sos) pred1)  1)))

      (let ((pred nil))
        (dolist (clause clauses)
            (when (or (search (list (second sos)) clause)
			(search (list (first sos)) clause))
            (setq pred clause)
			(cond ((= (length sos) 1)  (setq notpos (- (position (first sos) pred) 1)))
                 	             (t (setq notpos (- (position (second sos) pred) 1))))
			(print pred)		
		(cond ((and (equal (length sos) 2)
		(member (second sos) pred)
		(not (equal(nth notpos pred) 'not))) 
		(setq output (remove (second sos) pred))
		(setq output (rest output))
		(setq soslist (append (list output) soslist))
		(setq sos output)
		(nconc clauses (list sos))
		(setq pred1 (first (rest clauses))))
           
		((and (equal (length sos) 1)
          (member (first sos) pred)
		  (equal(nth notpos pred) 'not))
		(setq output (remove-at pred (+ notpos 1)))
		(setq output (remove (first sos) output))
		(setq output (rest output))
		(setq soslist (append (list output) soslist))
		(setq sos output)
		(nconc clauses (list sos))
		(setq pred1 (first (rest clauses)))
		(print sos))

		(t (print "Found contradiction!")))))

)))


Re: can we search 2 lists at a time?

Posted: Tue Apr 27, 2010 11:38 am
by prathyuguduri
@Jasper

Sorry but I don't understand your function completely. What is the "about" that you are using. I don't use "and". And you are comparing only and, or & not. I need to compare the symbols also, in the process I get new elements that I need to append to the list and search in them also until i get a null when i resolve(i.e a (f) & (not f) which should give nil) and then I stop the function.

Re: can we search 2 lists at a time?

Posted: Wed Apr 28, 2010 5:25 am
by Jasper
Essentially(for default TEST), it assigns a true/false value based on a symbol(about) and clause. If the clause is (OR &rest rest) then it returns true only if one or more of the elements of REST os equal to the symbol, or one of the clauses returns true. Similarly for AND, where all of them must be true, and NOT where all of them must be false. Some examples:

Code: Select all

(clause-says 'a 'a) ;-> EQL so true
(clause-says 'a '(or b c d)) ; none of them is A, false.
(clause-says 'a '(not c d)) ; none of them is A, true
(clause-says 'a '(or (not a) (and b c d))) ;False

Re: can we search 2 lists at a time?

Posted: Sat May 08, 2010 9:29 am
by dlweinreb
I'm not sure I understand what's going on here, but if you want dolist to iterate over list a, and then if it's still iterating, iterate over list b, you can usually do:

(dolist (var (append list-1 list-2))
.. body ..)