[ Starting this in spite of SWITCH still being on the table, trying to clear some others into blessed state. ]
macro MULTIPLE-VALUE-PROG2 first-form second-form &body forms
"Evaluates FIRST-FORM, then SECOND-FORM, and then FORMS. Yields as its value all the value returned by SECOND-FORM."
macro NTH-VALUE-OR nth-value &body forms
"Evaluates FORM arguments one at a time, until the NTH-VALUE returned by one of the forms is true. It then returns all the values returned by evaluating that form. If none of the forms return a true nth value, this form returns NIL."
macro WHICHEVER &rest possibilities
"Evaluates exactly one of POSSIBILITIES, chosen at random."
macro XOR &rest datums
"Evaluates its arguments one at a time, from left to right. If more then one argument evaluates to a true value no further DATUMS are evaluated, and NIL is returned as both primary and secondary value. If exactly one argument evaluates to true, its value is returned as the primary value after all the arguments have been evaluated, and T is returned as the secondary value. If no arguments evaluate to true NIL is retuned as primary, and T as secondary value."
All seem fine to me -- both in terms of docstring and implementation, and they even have tests of sort. Unless there are objections during this week, I'll move them into blessed section in package.lisp.
Cheers,
-- Nikodemus
On 2010/04/05, at 18:55 , Nikodemus Siivola wrote:
macro WHICHEVER &rest possibilities
"Evaluates exactly one of POSSIBILITIES, chosen at random."
ONE-OF seems to be often used not name a similar function.
I understand the need for a macro version of the feature, but could we name it in a similar way?
Could we include the ONE-OF function too?
If we included a function named ONE-OF, perhaps we could name the macro EVAL-ONE-OF ? (think EVAL-WHEN, not EVAL).
macro XOR &rest datums
"Evaluates its arguments one at a time, from left to right. If more then one argument evaluates to a true value no further DATUMS are evaluated, and NIL is returned as both primary and secondary value. If exactly one argument evaluates to true, its value is returned as the primary value after all the arguments have been evaluated, and T is returned as the secondary value. If no arguments evaluate to true NIL is retuned as primary, and T as secondary value."
There's a definition of a n-ary XOR that returns true when there's an odd number of true arguments. In which case no short-cut can be taken and it's defined as a function.
It seems to me that n-ary AND and OR, are defined like n-ary * and +, that is with associativity in mind (of the mathematician who thinks about them).
(and a b c) = (and (and a b) c) (or a b c) = (or (or a b) c)
not as (and a b c) = all are true, (or a b c) = at least one is true.
Wikipedia, which as an "encyclopedia" tries to gather the common knowledge, also defines n-ary XOR as odd number of true: http://en.wikipedia.org/wiki/Exclusive_or which is compatible with (xor a b c) = (xor (xor a b) c).
The above proposed definition would break this identity.
;; (prototypes functions to demonstrate the 'truth' value:) [232]> (defun xor-at-most-one (&rest args) (and (<= 1 (count-if (function identity) args)) (find-if (function identity) args))) XOR-AT-MOST-ONE [233]> (xor-at-most-one nil 1 2 3 4) 1 [234]> (defun xor-odd (&rest args) (oddp (count-if (function identity) args))) XOR-ODD [235]> (xor-odd nil 1 2 3 4) NIL
I'm not saying that a macro like the above XOR is not useful, just that I don't like it clobbering the good XOR name.
If it was called something like:
THE-ONE THE-ONLY-ONE FIRST-AND-ONLY
it would be ok for me.
On the other hand I would definitely like to have a function named XOR defined as returning true when there's an odd number of true arguments. If you can find a good name similarly to EVERY and SOME for such a function, then I would agree to relinquish XOR for this macro in consistency with AND/EVERY and OR/SOME. ( But notice that NOT is a function in CL, and (NOT x) = (XOR T x) ;-) )
Somebody with a free little time could build an index of the names of all the functions and macro defined in all the Lisp code he can find, and identify (similar) names occuring serveral times covering the same or similar features? It would be advantageous to reuse these names (and perhaps implementations). I would see Alexandria as the Common (Lisp Library) instead of the (Common Lisp) Library.
Sorry, forgot to CC alexandria-devel
2010/4/5 Pascal J. Bourguignon pjb@informatimago.com
On 2010/04/05, at 18:55 , Nikodemus Siivola wrote:
macro WHICHEVER &rest possibilities
"Evaluates exactly one of POSSIBILITIES, chosen at random."
ONE-OF seems to be often used not name a similar function.
I understand the need for a macro version of the feature, but could we name it in a similar way?
Could we include the ONE-OF function too?
If we included a function named ONE-OF, perhaps we could name the macro EVAL-ONE-OF ? (think EVAL-WHEN, not EVAL).
As a functional version of *whichever *there is already *random-elt*. I can't think of an implementation of *one-of* that would not need to build a list at compile time, so the only difference between *one-of* and * random-elt* is if the construction is made explicitly by the user or implicitly.
As for the name, I believe *whichever** *is a good name and should not be changed.
alexandria-devel@common-lisp.net