Page 1 of 2

OR between list elements

Posted: Wed Jul 04, 2012 1:53 am
by mparsa
If I have a list for example (nil t nil) and I want to OR the elments of this list, I mean nil or t or nil and finally get the answer "t". what is the best way to do that !!?

Re: OR between list elements

Posted: Wed Jul 04, 2012 2:26 am
by Goheeca
There are plenty of ways how to do that:

Code: Select all

(some #'identity list)

Code: Select all

(reduce #'(lambda (a b) (or a b)) list)

Code: Select all

(loop for item in list thereis item)
for the and operation it's analogous:

Code: Select all

(every #'identity list)

Code: Select all

(reduce #'(lambda (a b) (and a b)) list)

Code: Select all

(loop for item in list always item)

Re: OR between list elements

Posted: Wed Jul 04, 2012 4:52 am
by mparsa
Thank you for the answer. I still have problem because for exapmle when I use (some #'or (list 0 1)) or I use reduce besides "some". It gives me an error : rror: or names a macro -- bad arg for function.
It works fine with "*" or other operation but it doesn't work with "or" !!

Re: OR between list elements

Posted: Wed Jul 04, 2012 5:04 am
by Goheeca
Because or isn't a function it's a macro and it must be because of lazy evaluation of its arguments. And functions like apply, reduce, etc. accept only a function not a macro.

Re: OR between list elements

Posted: Wed Jul 04, 2012 5:10 am
by mparsa
So what can I do now !?

Re: OR between list elements

Posted: Wed Jul 04, 2012 5:30 am
by Goheeca
It depends on what exactly you want, what the issue requires ...
You can write own or-function (Take note that evaluate all arguments against an ordinary or):

Code: Select all

(defun my-or (&rest rest)
  (eval `(or ,@rest)))
But it's better to avoid redundant eval calls.

Re: OR between list elements

Posted: Wed Jul 04, 2012 5:59 am
by JamesF
What's the actual problem you're trying to solve? There may be a better tool for the job; CL is like Unix - it's been around for long enough that there's usually something built-in that does what you need.

Re: OR between list elements

Posted: Thu Jul 05, 2012 12:14 am
by mparsa
I just want to use "OR" operation between the element of the list. something like this (OR (nil , t , t, nil)) So the return value in this example should be t because nil OR t OR t OR nil is t.

Re: OR between list elements

Posted: Thu Jul 05, 2012 12:36 am
by JamesF
Sorry, I should have been clearer. It looks like you've settled on this as the solution to whatever problem you're solving, and you're now asking for help with implementing it. We can see that you're having trouble with the use of #'or, but I'm asking what higher problem you're trying to solve by applying it to a list.

Goheeca suggested a variety of alternative solutions, but you seem fixated on using #'or - is there a particular reason you're intent on using that operator? Could this be a homework assignment, by chance?

Re: OR between list elements

Posted: Thu Jul 05, 2012 1:03 am
by mparsa
No , it is not the homework assignment, I explained the problem again because you asked me to explain. So I thought you didn't get what I mean. I tried the first solution that he sujjested but it didn't work. I didn't try the second one yet ! It is a part of the project so I need to "OR" the eleemnts of the array in any way, no matter how I implement it because the aim of the project is something else !!!