[plt-scheme] struct-eqv?

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Tue Mar 10 20:47:39 EDT 2009

At Tue, 10 Mar 2009 18:14:27 -0600, Chongkai Zhu wrote:
> Stephen De Gabrielle wrote:
> > but is there a predicate for determining two different structs have
> > the same field values?
>
> Here I assume you are asking about struct-equal? instead of struct-eqv?.
> 
> None is build-in, but you can write one as:
> 
> (define (struct-equal? v1 v2)
>   (equal? (struct->vector v1)
>           (struct->vector v2)))

Actually, when that works, you can just use `equal?' directly on the
structures.

But `equal?' works only for transparent structure types, such as

 (define-struct point (x y) #:transparent)


If you want to keep a structure type opaque, you can specialize
`equal?' by implementing the `prop:equal+hash' property:

 (define-struct point (x y)
   #:property prop:equal+hash
              (list
               (lambda (a b equal?-recur) 
                 (and (equal?-recur (point-x a) (point-x b))
                      (equal?-recur (point-y a) (point-y b))))
               (lambda (a hash-code-recur)
                 (+ (hash-code-recur (point-x a))
                    (* 3 (hash-code-recur (point-x b)))))
               (lambda (a hash2-code-recur)
                 (+ (hash2-code-recur (point-x a))
                    (hash2-code-recur (point-x b))))))



Posted on the users mailing list.