|
Prev: Lisp and Web Programming
Next: Koch figures
From: Hannah Schroeter on 13 Jul 2005 13:35 Hello! Russell McManus <russell_mcmanus(a)yahoo.com> wrote: >[...] >I think this works: >(defmacro xor (v1 v2) > `(not (eq (not ,v1) (not ,v2)))) Use macros only if functions don't work. (defun xor (v1 v2) (not (eq (not v1) (not v2)))) >-russ Kind regards, Hannah.
From: Frank Buss on 13 Jul 2005 17:00 Christophe Rhodes wrote: > You're not necessarily wrong, but note that AND and OR are macros, not > functions, because they short-circuit; also, they take arbitrary > numbers of arguments, not just two; so it's not clear that XOR > actually fits in with AND and OR. a nice specification is given by the ext:xor function in CLisp: | This function checks that exactly one of its arguments is non-NIL and, | if this is the case, returns its value and index in the argument list | as multiple values, otherwise returns NIL. An implementation by me: (defun xor (&rest list) (when list (destructuring-bind (first &rest rest) list (if first (when (every #'null rest) first) (apply #'xor rest))))) I'm sure it could be done easier :-) -- Frank Buý, fb(a)frank-buss.de http://www.frank-buss.de, http://www.it4-systems.de
From: Christophe Rhodes on 13 Jul 2005 17:04 Frank Buss <fb(a)frank-buss.de> writes: > a nice specification is given by the ext:xor function in CLisp: > > | This function checks that exactly one of its arguments is non-NIL and, > | if this is the case, returns its value and index in the argument list > | as multiple values, otherwise returns NIL. Why is this nice? Christophe
From: Frank Buss on 13 Jul 2005 17:06 Frank Buss wrote: >| This function checks that exactly one of its arguments is non-NIL and, >| if this is the case, returns its value and index in the argument list >| as multiple values, otherwise returns NIL. > > An implementation by me: I forgot the index, so some standard loop magic: (defun xor (&rest list) (when list (loop for (first . rest) on list for i = 0 then (1+ i) do (if first (when (every #'null rest) (return (values first i))))))) -- Frank Buý, fb(a)frank-buss.de http://www.frank-buss.de, http://www.it4-systems.de
From: jtdubs on 13 Jul 2005 18:02
Philippe Lorin wrote: > I'm looking for a XOR to use like I use AND and OR. But the only things > I found in the CLHS are: > BIT-XOR, which works on bit arrays > BOOLE-XOR, which, when used with BOOLE, works on integers > LOG-XOR, which works on integers > > CLisp seems to have XOR, but CMUCL and SBCL don't. Isn't there some > standard function for this? Or am I wrong in wanting one? > > Background: I need to check that two values are either both null or both > non-null, and not a mix of null and non-null. I'd like to write: > (assert (not (xor (null v1) (null v2)))) Assuming v1 and v2 are boolean: (xor v1 v2) == (not (eq v1 v2)) (xnor v1 v2) == (eq v1 v2) So, in your case I would write: (assert (eq (null v1) (null v2))) As #'AND and #'OR are both macros that take any number of arguments, I'm not sure that XOR really does fit in with them. However, it is trivially implementable as a function because it must evaluate all of its arguments. Justin Dubs |