Better eval.lisp

This commit is contained in:
2026-04-17 02:40:20 -07:00
parent 5c73f19177
commit c744d397fa

View File

@@ -45,31 +45,20 @@ and the maximum number (or nil for infinity) as a second value."
(t (error "unknown operator: ~S" oper)))) (t (error "unknown operator: ~S" oper))))
(defun logical-xor (&rest args) (defun logical-xor (&rest args)
"Logical xor (not equal) each argument in turn with its following argument. "Logical xor (not equal) each argument in turn with its following argument."
NOTE: This is NOT a macro, there is no short circuit evaluation (all arguments (eql 1 (count-if #'identity args)))
are evaluated no matter what)."
(loop with result = nil
for arg in args do
(setq result (not (eq result arg)))
finally (return result)))
(defun logical-and (&rest args) (defun logical-and (&rest args)
"Logical and (all true). "Logical and (all true)."
NOTE: This is NOT a macro, there is no short circuit evaluation (all arguments (every #'identity args))
are evaluated no matter what)."
(not (member nil args)))
(defun logical-or (&rest args) (defun logical-or (&rest args)
"Logical or (one or more true). "Logical or (one or more true)."
NOTE: This is NOT a macro, so there is no short circuit evaluation (all (some #'identity args))
arguments are evaluated no matter what)."
(not (not (member t args))))
(defun logical-implies (prop1 prop2) (defun logical-implies (prop1 prop2)
"Evaluate the logical implies operation on PROP1 and PROP2. That is \"if "Evaluate the logical implies operation on PROP1 and PROP2. That is \"if
PROP1, then PROP2\". PROP1, then PROP2\"."
NOTE: This is NOT a macro, so there is no short circuit evaluation (all
arguments are evaluated no matter what)."
(if prop1 ;; only if first is true (if prop1 ;; only if first is true
prop2 ;; eval second prop2 ;; eval second
t)) ;; otherwise, just return true t)) ;; otherwise, just return true