For a technical explanation of how eq? works eq? , take a look at the current specification , you will not find a more detailed link, Or just check the Racket documentation on this subject, in particular the eq? procedure eq? eqv? and equal? . As for your question - the result will be the same expected and correct in the code of the Scheme, let's see why. Note that in this line in Java:
p1.x = 42;
You are modifying the same object that both p1 and p2 point to. If in this line:
(set! cons1 (cons 2 empty))
You create a new, different object and set cons1 to point to it, but cons2 still points to the old object. You can confirm this, after the previous line, comparison (eq? cons1 cons2) will return #f .
The fact is that the examples are not equivalent. A Java example refers to a single object that is referenced by two different links, while a sample example considers two objects and two links.
For comparison, here is an example of a scheme similar to Java code, and it works as you expected, because here we modify one mutable object that is referenced by two links:
#lang racket (require scheme/mpair) ;; `m` stands for "mutable" (define p1 (mlist 5 5)) (define p2 p1) (eq? p1 p2) ;; #t (mcar p1) ;; 5 (mcar p2) ;; 5 (set-mcar! p1 42) (eq? p1 p2) ;; #t (mcar p1) ;; 42 (mcar p2) ;; 42
Óscar López
source share