OR between list elements

Discussion of Common Lisp

OR between list elements

Postby mparsa » Wed Jul 04, 2012 1:53 am

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 !!?
mparsa
 
Posts: 26
Joined: Mon Jun 25, 2012 6:15 am

Re: OR between list elements

Postby Goheeca » Wed Jul 04, 2012 2:26 am

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)
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version).
User avatar
Goheeca
 
Posts: 220
Joined: Thu May 10, 2012 12:54 pm

Re: OR between list elements

Postby mparsa » Wed Jul 04, 2012 4:52 am

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" !!
mparsa
 
Posts: 26
Joined: Mon Jun 25, 2012 6:15 am

Re: OR between list elements

Postby Goheeca » Wed Jul 04, 2012 5:04 am

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).
User avatar
Goheeca
 
Posts: 220
Joined: Thu May 10, 2012 12:54 pm

Re: OR between list elements

Postby mparsa » Wed Jul 04, 2012 5:10 am

So what can I do now !?
mparsa
 
Posts: 26
Joined: Mon Jun 25, 2012 6:15 am

Re: OR between list elements

Postby Goheeca » Wed Jul 04, 2012 5:30 am

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.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version).
User avatar
Goheeca
 
Posts: 220
Joined: Thu May 10, 2012 12:54 pm

Re: OR between list elements

Postby JamesF » Wed Jul 04, 2012 5:59 am

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.
JamesF
 
Posts: 95
Joined: Thu Jul 10, 2008 7:14 pm

Re: OR between list elements

Postby mparsa » Thu Jul 05, 2012 12:14 am

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.
mparsa
 
Posts: 26
Joined: Mon Jun 25, 2012 6:15 am

Re: OR between list elements

Postby JamesF » Thu Jul 05, 2012 12:36 am

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?
JamesF
 
Posts: 95
Joined: Thu Jul 10, 2008 7:14 pm

Re: OR between list elements

Postby mparsa » Thu Jul 05, 2012 1:03 am

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 !!!
mparsa
 
Posts: 26
Joined: Mon Jun 25, 2012 6:15 am

Next

Return to Common Lisp

Who is online

Users browsing this forum: Bing [Bot] and 4 guests