Why is (equal (copy-<some struct type> <struct type object>) <same struct type object>) nil?

Viewed 45

The elements of the struct type are s-expressions.

(defstruct state
  homeS
  homeH
  homeD
  homeC
  free
  stacks)

I was attempting to use these objects as keys to a hash table. Before I converted to a struct and was using a deeper S-expression, all worked well. When I changed to a defstruct, the hash table never succeeded with finding a duplicate key.

The hashtable was made with (make-hash-table :test #'equal), so I looked at the the behavior of equal.

I expected (equal (copy-state state1) state1) to return t, but it returned nil.

I haven't found this in the Common Lisp Hyper-Specification. I think the answer is to write my own test and hash functions, and give them to make-hash-table.

This must be defined as common lisp behavior -- it works the same way in CLISP and SBCL.

1 Answers

According to the Common Lisp Hyper Specification, the natural operation "equal", does not work with structured data like defstruct and defarray objects.

In this case, for objects defined by a defstruct type the "equal" operator does not work, and a different operator, "equalp", is used to compare structured data.

Related