emacs-devel
[Top][All Lists]
Advanced

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

Re: 7 logical-xor implementations in source tree


From: Mattias Engdegård
Subject: Re: 7 logical-xor implementations in source tree
Date: Tue, 23 Jul 2019 18:41:09 +0200

23 juli 2019 kl. 14.38 skrev Stefan Monnier <address@hidden>:
> 
> I'm not convinced it's worth the trouble (we only have 7 uses so far
> and they all seem happy with a 2-arg xor).
> Especially since there are 2 different reasonable semantics.
> It can always be extended later if needed.

That's reasonable. Racket made it 2-arg (thanks for the reference, Basil); 
looks like they couldn't make up their minds either.

>> * Give it a compiler macro, for efficient partial application
> 
> Given how rarely it's used, I'm not sure it's worth the trouble.
> Luckily if we only accept the 2-args case this can be done very cheaply
> with defsubst or define-inline.

Yes, define-inline in particular will produce decent code.

I would probably use the negation, boolean equivalence, more often than xor 
myself.
Since `equiv' (placeholder name) is more readily thought of as an equivalence 
predicate, its n-ary semantics is subject to less debate.

Example implementations:

+(define-inline xor (arg1 arg2)
+  "Boolean exclusive-or: the non-nil argument if the other is nil, else nil."
+  (inline-letevals (arg1 arg2)
+    (inline-quote
+     (if ,arg1
+         (if ,arg2
+             nil
+           ,arg1)
+       ,arg2))))
+
+(defmacro equiv (&rest args)
+  "Boolean equivalence: t if arguments are all non-nil or all nil."
+  (cond ((null args) t)
+        ((null (cdr args)) `(progn ,(car args) t))
+        (t `(if ,(car args)
+                (and ,@(append (cdr args) '(t)))
+              (not (or ,@(cdr args)))))))




reply via email to

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