is it possible to write a strict AND function?

Discussion of Common Lisp

Re: is it possible to write a strict AND function?

Postby gugamilare » Sun Jul 10, 2011 3:48 pm

wvxvw wrote:Even though 'or isn't a function you are allowed to use it with 'every, so it will work as advertised (at least it worked for me in SBCL and CLISP).


Are you sure? It didn't work for me in SBCL, and the Hyperspec says the predicate must be a function designator.

wvxvw wrote:As an aside, I don't quite understand why is original 'and returns true if given no arguments, or why is there such an option at all. (That's to say I don't understand, not that it's wrong).


Calling (or) is equivalent to asking: Is there some element of the empty list that classifies as true? Since there is nothing in the empty list, in particular there is nothing that classifies as true, so the answer is false.

Calling (and) is equivalent to asking: Do all elements of the empty list classify as true? That is true by vacuity. In logic, answering no to that question is the same as saying that there is an element in the empty list that classifies as false, that is, the empty list contains nil. Since that is obviously not true, the answer to the question is yes.
gugamilare
 
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil

Re: is it possible to write a strict AND function?

Postby wvxvw » Mon Jul 11, 2011 1:23 am

I seriously did try it, though on Window machine, would that be an issue, also I remember that I tried it after with 'apply, and it didn't, so it was kind of strange? I could even post a screenshot of it working once I'm back there :) But yes, I tried it now on Linux SBCL and it didn't work. Yes, it makes sense what you say. Also I remember that in error it gave about 'or it didn't mention it as a macro, it said it is a special operator, which would still qualify as a function probably?
wvxvw
 
Posts: 125
Joined: Sat Mar 26, 2011 6:23 am

Re: is it possible to write a strict AND function?

Postby gugamilare » Mon Jul 11, 2011 4:29 am

wvxvw wrote:I seriously did try it, though on Window machine,


I was on Windows when I tried it :?

wvxvw wrote:[...] Also I remember that in error it gave about 'or it didn't mention it as a macro, it said it is a special operator, which would still qualify as a function probably?


But or is a macro.
gugamilare
 
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil

Re: is it possible to write a strict AND function?

Postby yesimthetaxman » Mon Jul 11, 2011 9:38 am

wow there are so many ideas, thank you.

I think the problem with this question is that it is suppose to avoid higher order functions that do the work for you... apparently there is an elegant solution that uses only basic functions, no macros. once I figure it out I will post it here. I don't think functions such as "every" can be used. i think reduce is allowed though
yesimthetaxman
 
Posts: 10
Joined: Thu Jul 07, 2011 7:02 pm

Re: is it possible to write a strict AND function?

Postby yesimthetaxman » Sun Jul 24, 2011 5:06 pm

turns out that the answer is actually quite simple:

(defun and-nl (a b) (and a b))

since arguments are evaluated before they are passed to the routine body we have an AND that forces the evaluation of both arguments to AND.
yesimthetaxman
 
Posts: 10
Joined: Thu Jul 07, 2011 7:02 pm

Re: is it possible to write a strict AND function?

Postby gugamilare » Sun Jul 24, 2011 7:27 pm

yesimthetaxman wrote:turns out that the answer is actually quite simple:

(defun and-nl (a b) (and a b))

since arguments are evaluated before they are passed to the routine body we have an AND that forces the evaluation of both arguments to AND.


Well, yes. I and many others assumed you wanted a function that would take as many arguments as you wanted, just like the macro and, which takes up from 0 arguments.
gugamilare
 
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil

Re: is it possible to write a strict AND function?

Postby Paul » Sun Jul 24, 2011 8:49 pm

yesimthetaxman wrote:turns out that the answer is actually quite simple:

(defun and-nl (a b) (and a b))

since arguments are evaluated before they are passed to the routine body we have an AND that forces the evaluation of both arguments to AND.


Fourth post in this thread :)
Paul
 
Posts: 106
Joined: Tue Jun 02, 2009 6:00 am

Re: is it possible to write a strict AND function?

Postby yesimthetaxman » Mon Jul 25, 2011 9:19 am

ohh ya your right i didn't realize that lol
yesimthetaxman
 
Posts: 10
Joined: Thu Jul 07, 2011 7:02 pm

Previous

Return to Common Lisp

Who is online

Users browsing this forum: Google [Bot] and 2 guests

cron