OR between list elements
OR between list elements
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:
for the and operation it's analogous:
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)
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)
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" !!
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):
But it's better to avoid redundant eval calls.
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)))
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?
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 !!!