This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

OR between list elements

14 posts · 10514 views

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

There are plenty of ways how to do that:
(some #'identity list)
(reduce #'(lambda (a b) (or a b)) list)
(loop for item in list thereis item)
for the and operation it's analogous:
(every #'identity list)
(reduce #'(lambda (a b) (and a b)) list)
(loop for item in list always item)
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

Re: OR between list elements

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

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.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

Re: OR between list elements

So what can I do now !?

Re: OR between list elements

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):
(defun my-or (&rest rest)
  (eval `(or ,@rest)))
But it's better to avoid redundant eval calls.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

Re: OR between list elements

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

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

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

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

Re: OR between list elements

Sorry if I gave offense; we do get a small but regular stream of people trying to outsource their homework assignments, and it's pretty unusual that a problem requires a specific function or operator, so I may have leapt to a conclusion.

You've explained the solution that you're trying to apply and that you're having trouble with, not the problem that you're trying to solve with it.
If you need to check whether one or more of the elements in the list is non-null, then any of Goheeca's suggestions will work - as long as you don't try to shoehorn #"or where it won't fit.

To ask from another angle: what behaviour is it that you need, that only the #'or operator provides?
Perhaps you could tell us what the aim of the project is? That would help us to give you more appropriate advice.

Re: OR between list elements

No problem. But I just started to program in lisp less that 2 week ago so I don't know even the simple syntax. Remind yourself when you started to program in lisp. ;)
I also search for everything that I need but I put my question here for the best solution.
Now I define the function for what I need like this :
(defun my-or (li)
(setf k nil)
(loop for l in li collecting (setf k (or k l)))
)
It works now. But I thought maybe there is a direct solution without defining the new function.
Anyway thank you for your time :)

Re: OR between list elements

(some #'identity (list nil nil t nil))
I think it's a direct solution. Try it again, you haven't tried it exactly this way probably and rather something like this:
(some #'or (list nil nil t nil))
which is bad.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

Re: OR between list elements

yes. My bad. I used or besides identity. Now it works.
Thanks in advance.