From: Philippe Lorin on
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))))
From: Christophe Rhodes on
Philippe Lorin <palpalpalpal(a)gmail.com> writes:

> CLisp seems to have XOR, but CMUCL and SBCL don't. Isn't there some
> standard function for this?

No.

> Or am I wrong in wanting one?

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.

> 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))))

(assert (if v1 v2 (not v2)))

This is easily wrapped up into a function if you feel the need.

Christophe
From: Frank Buss on
Philippe Lorin wrote:

> 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))))

what about (eq (null v1) (null v2)) ?

--
Frank BuĂ˝, fb(a)frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
From: Philippe Lorin on
Frank Buss wrote:
> what about (eq (null v1) (null v2)) ?

This one is nice, it conveys the intention perfectly. Thanks!
From: Russell McManus on
Philippe Lorin <palpalpalpal(a)gmail.com> writes:

> 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?

I think this works:

(defmacro xor (v1 v2)
`(not (eq (not ,v1) (not ,v2))))

-russ
 |  Next  |  Last
Pages: 1 2 3 4 5 6 7 8 9
Prev: Lisp and Web Programming
Next: Koch figures