Equivalence Tester

I finally got an equivalence tester for Common Lisp created. The code can be found by following the previous link or by reading more. Here's its description:

This method is used to compare classes. Override it to specialize it's testing for your classes. There's already a method specialized for built in classes that uses EQUAL, cons cells, and any standard object.
;;; The following equivalence checker was created by Nolan Eakins,
;;; http://nolan.eakins.net/ . This code can be treated as if it's
;;; public domain or public domain plus an attribution. This requires
;;; Gary King's Moputilities.

(defgeneric eqv (a b)
  (:documentation "This method is used to compare classes. Override it
to specialize it's testing for your classes. There's already a method
specialized for built in classes that uses EQUAL, cons cells, and any
standard object."))

(defmethod eqv (a b)
  (equal a b))

(defmethod eqv ((a cons) (b cons))
  (cond
    ((and a b)
     (and (eqv (first a) (first b))
          (eqv (rest a) (rest b))))
    (t
     nil)))

(defun and-list (list)
  ;; Thanks to Zach Beane for simplifying this
  (every #'identity list))

(defmethod eqv ((a standard-object) (b standard-object))
  (when (eq (type-of a) (type-of b))
    (and-list
     (mapcar #'(lambda (slot)
                 (eqv (slot-value a slot)
                      (slot-value b slot)))
             (mopu:slot-names (find-class (type-of a)))))))
        

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

(defun and-list (list)

(defun and-list (list) (every #'identity list))

Thanks

Good call on that. I've updated it above.

Ad's by Google