can we search 2 lists at a time?

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

can we search 2 lists at a time?

Post by prathyuguduri » Tue Apr 27, 2010 12:29 am

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.

Jasper
Posts: 209
Joined: Fri Oct 10, 2008 8:22 am
Location: Eindhoven, The Netherlands
Contact:

Re: can we search 2 lists at a time?

Post by Jasper » Tue Apr 27, 2010 7:38 am

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.

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

Re: can we search 2 lists at a time?

Post by nuntius » Tue Apr 27, 2010 7:47 am

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).

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

Re: can we search 2 lists at a time?

Post by prathyuguduri » Tue Apr 27, 2010 10:24 am

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!")))))

)))

Last edited by prathyuguduri on Tue Apr 27, 2010 1:29 pm, edited 1 time in total.

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

Re: can we search 2 lists at a time?

Post by prathyuguduri » Tue Apr 27, 2010 11:38 am

@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.

Jasper
Posts: 209
Joined: Fri Oct 10, 2008 8:22 am
Location: Eindhoven, The Netherlands
Contact:

Re: can we search 2 lists at a time?

Post by Jasper » Wed Apr 28, 2010 5:25 am

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

dlweinreb
Posts: 41
Joined: Tue Jul 01, 2008 5:11 am
Location: Lexington, MA
Contact:

Re: can we search 2 lists at a time?

Post by dlweinreb » Sat May 08, 2010 9:29 am

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 ..)

Post Reply