Compare - Examine Object Equality
Posted: Tue Nov 03, 2009 4:36 am
Hey, I've got this function I think some people might be interested in. I've put it into Lisp Paste: http://paste.lisp.org/+1X85, and below:
Please let me know what you think of it, if you have something similar, of any improvements, etc.
Code: Select all
(defun compare (a b &optional (out-stream t))
"This function compares a and b for equality. The third argument is where to output the result."
(flet ((outty (string &rest args)
(apply #'format out-stream string a b args)))
(cond
((eq a b) (outty "The objects ~S & ~S are the same object (eq)."))
((eql a b)
(typecase a
(number (outty "The numbers ~S & ~S are the same number type and value (eql)."))
(character (outty "The characters ~S & ~S are the same character (eql)."))
(t (outty "The objects ~S & ~S should be eq, but they got to the eql cond."))))
((equal a b)
(typecase a
(cons (outty "The conses ~S & ~S are recursively equal (equal)."))
(string (outty "The strings ~S & ~S contain the same characters (equal, case-sensitive)."))
((vector bit) (outty "The bit vectors ~S & ~S contain the same bits (equal)."))
(pathname (outty "The pathnames ~S & ~S are functionally equivalent (equal)."))
(t (outty "The objects ~S & ~S should be eql or eq, but they got through those (equal)."))))
((equalp a b)
(typecase a
(cons (outty "The conses ~S & ~S are recursively equalp (equalp)."))
(character (outty "The characters ~S & ~S are char-equal (equalp, case-insensitive)."))
(number (outty "The numbers ~S & ~S represent the same value (equalp, =)."))
(array (outty "The arrays ~S & ~S contain the same elements (equalp)."))
(t (outty "The objects ~S & ~S are of type ~S and are equalp." (type-of a)))))
((equalp (type-of a) (type-of b))
(outty "The objects ~S & ~S are not the same, but are the same type."))
(t (outty "The objects ~S & ~S are not the same in any way (not even type)."))))
(terpri out-stream))