[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: 7 logical-xor implementations in source tree

From: Juri Linkov
Subject: Re: 7 logical-xor implementations in source tree
Date: Fri, 26 Jul 2019 00:46:22 +0300
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (x86_64-pc-linux-gnu)

> That depends on the preferred semantics, right?  What semantics are you
> suggesting?  Judging from online discussions I've come across, there's
> more than one way to skin this cat.

Shouldn't xor have an associative property:

  (xor a b c) ≡ (xor a (xor b c)) ≡ (xor (xor a b) c)

> OTOH, a definition like the following:
>   (defsubst xor (cond1 cond2)
>     (cond ((not cond1) cond2)
>           ((not cond2) cond1)))
> not only performs simple logical xor more efficiently, but also returns
> whichever of the two arguments is non-nil (though I can't imagine when
> this last detail would ever be useful).

Returning a non-nil argument as-is is a useful property.
Most implementations lack this property, so better to use:

  (unless (and a b) (or a b))

But the implementation with 'cond' is nice too.

Another useful property is to evaluate arguments only once
that currently is not the case:

  (defvar a 0)
  (defvar b 0)
  (defmacro xor (a b) `(cond ((not ,a) ,b) ((not ,b) ,a)))
  (xor (setq a (1+ a)) nil)
  ⇒ 2

But I don't know if an implementation with such property
is possible at all (without using temporary variables).

reply via email to

[Prev in Thread] Current Thread [Next in Thread]