Pergunta

Suppose I have the following Scheme (R6RS) code:

(define-record-type typeA
 (fields
  (mutable A)))

and that I create two records:

(define X (make-typeA 123))
(define Y (make-typeA 123))

I can't understand why (equal? X Y) and (equal? (make-typeA 123) (make-typeA 123)) return #f.

I read the R6RS standard (section 11.5) but I didn't really understand it.

Foi útil?

Solução

From R6RS:

The equal? predicate treats pairs and vectors as nodes with outgoing edges, uses string=? to compare strings, uses bytevector=? to compare bytevectors (see library chapter on “Bytevectors”), and uses eqv? to compare other nodes.

In other words, your use of equal? is really just doing the same thing as eqv? since that's how it's specified for records (see last line of above).

Chapter 6.1 has this to say about eqv? of records:

If obj1 and obj2 are both records of the same record type, and are the results of two separate calls to record constructors, then eqv? returns #f.

In your code, the two records are the results of two separate calls to the constructor. Hence they cannot be eqv?.

Some dialects of Scheme may allow you to use structural equality on records. For example, in Racket you can declare a record as #:transparent to get structural equality. I'm not sure you can get this behavior in standard Scheme.

Outras dicas

I'm not really familiar with R6RS, but a quick google search suggests that

the key thing here is mutability:

If obj1 and obj2 are both records of the same record type, and are the results of two separate calls to record constructors, then eqv? returns #f.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top