Scheme box and pointer cyclic list

Viewed 101

I am trying to understand what is happening when writing this code by drawing it with the box and pointer method but cant quite get it. Anybody that could help?

(define bar (list 'a 'b 'c 'd 'e))
(set-cdr! (cdddr bar) (cdr bar))

My idea:

enter image description here

Also I don't know how to start to draw a diagram for the code under, I cant see where the points and boxes for (car) need to be.

(define bah (list 'bring 'a 'towel))
(set-car! bah (cdr bah)) 
1 Answers

This is the initial situation:

enter image description here

And this is the final situation:

enter image description here

If you try to print bah, it is printed as a three element list, whose first element is a two element list:

((a towel) a towel)

But in fact the two structures are shared, so that, for instance, if you do:

(set-car! (cddr bah) 'handkerchief)

bah will be printed as:

((a handkerchief) a handkerchief)
Related