Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Okay, let's step through your program line by line. I'm also assigning unique numbers (think of them as object addresses, if you're used to C-like languages) for each newly-created object so you can see what's what. :-)</p> <pre><code>(define x (list 1 2 3)) ; =&gt; #1 = (1 . #2), #2 = (2 . #3), #3 = (3 . ()) (define y (list 4 5)) ; =&gt; #4 = (4 . #5), #5 = (5 . ()) (define z (cons (car x) (cdr y))) ; =&gt; #6 = (1 . #5) (define w (append y z)) ; =&gt; #7 = (4 . #8), #8 = (5 . #6) (define v (cons (cdr x) (cdr y))) ; =&gt; #9 = (#2 . #5) (set-car! x 6) ; =&gt; #1 = (6 . #2) (set-car! y 7) ; =&gt; #4 = (7 . #5) (set-cdr! (cdr x) (list 8)) ; =&gt; #2 = (2 . #10), #10 = (8 . ()) </code></pre> <p>Now, let's look at your values (for each reference, use the last assigned value):</p> <pre><code>x ; #1 =&gt; (6 . #2) =&gt; (6 . (2 . #10)) =&gt; (6 2 8) y ; #4 =&gt; (7 . #5) =&gt; (7 5) z ; #6 =&gt; (1 . #5) =&gt; (1 5) w ; #7 =&gt; (4 . #8) =&gt; (4 . (5 . #6)) =&gt; (4 . (5 . (1 . #5))) =&gt; (4 5 1 5) v ; #9 =&gt; (#2 . #5) =&gt; ((2 . #10) 5) =&gt; ((2 8) 5) </code></pre> <hr> <p>Edit: I'm adding a diagram to explain my answer, since you can't have diagrams in a comment. I don't have time to make a diagram showing the values above, but this hopefully explains a few things.</p> <p><img src="https://i.stack.imgur.com/YoHf6.png" alt="Expression tree"></p> <p>Each pair has two "slots", <code>car</code> and <code>cdr</code>, represented as the left and right boxes in the diagram above. Each of these slots, as you see, has three possible things:</p> <ol> <li>An atom (a number in your examples, or symbols in the diagram, such as <code>let</code>, <code>s5</code>, and <code>sqrt</code>)</li> <li>A reference (represented as an arrow in the diagram)</li> <li>Null (represented as a black box in the diagram)</li> </ol> <p>You can put any of these in any of the slots. So, in my explanation above, each of the <code>#</code> items is an arrow, each of the non-<code>#</code> numbers is an atom, and each of the <code>()</code> is a black box. So, in the line</p> <pre><code>(define v (cons (cdr x) (cdr y))) </code></pre> <p>you are creating a pair, where the left-hand slot has the same contents as the right-hand slot of <code>x</code> (i.e., an arrow going to pair 2), and the right-hand slot has the same contents as the right-hand slot of <code>y</code> (an arrow going to pair 5). In other words, both boxes in <code>v</code> contain arrows, each going off a different pair.</p> <p>Hope this makes more sense. :-)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload