Different car behavior with many cars! on two lists that look the same

I got confused as a result of using set-car! to two lists that have the same structure, but were built differently.

So, here is a sample code for two lists m and n :

 (define m (cons (cons 'a '()) (cons (cons 'a '()) '()))) (display m)(newline) ; => ((a) (a)) (set-car! (cadr m) 'b) (display m)(newline) ; => ((a) (b)) (newline) (define n (list '(x) '(x))) (display n)(newline) ; => ((x) (x)) (set-car! (cadr n) 'y) (display n)(newline) ; => ((y) (y)) 

According to do display , both lists have the same structure, but why applying the same procedure to them leads to different behavior? Is this related to how they were built?

+4
source share
1 answer

The results should be the same for both lists. Which schema interpreter are you using? it seems to me that your interpreter is reusing the expression '(x) .

After the comments, it seems that this is actually an error in the interpreter. Both lists should behave the same after the changes.

-1
source

Source: https://habr.com/ru/post/1433154/


All Articles