[alexandria-devel] Review cycle 3: WHICHEVER, XOR
Pascal J. Bourguignon
pjb at informatimago.com
Mon Apr 5 20:14:36 UTC 2010
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.
--
__Pascal Bourguignon__
http://www.informatimago.com
More information about the alexandria-devel
mailing list