-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathequals.lisp
31 lines (24 loc) · 931 Bytes
/
equals.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
(defpackage :cl-lox/equals
(:export :equals)
(:documentation "Provide EQUALS, an CLOS-compatible equality function.")
(:use :cl)
(:import-from :closer-mop))
(in-package :cl-lox/equals)
(defgeneric equals (a b)
(:documentation "Return if A and B are equal.
Lists are EQUALS if all their elements are EQUALS.
Structures are EQUALS if their classes are EQ and their slot values
are all EQUALS.
Defaults to EQUALP."))
(defmethod equals (a b)
(equalp a b))
(defmethod equals ((a list) (b list))
(or (and (null a) (null b)) ;; recursive base case
(and (equals (car a) (car b))
(equals (cdr a) (cdr b)))))
(defmethod equals ((a structure-object) (b structure-object))
(and (eq (class-of a) (class-of b))
(every (lambda (slot)
(equals (slot-value a (closer-mop:slot-definition-name slot))
(slot-value b (closer-mop:slot-definition-name slot))))
(closer-mop:class-slots (class-of a)))))