SHOW:
|
|
- or go back to the newest paste.
| 1 | A list in lisp looks like this: | |
| 2 | ||
| 3 | List = [mptr 0] | |
| 4 | ||
| 5 | [mptr0] = | car0 | [mptr 1] | | |
| 6 | [mptr1] = | car1 | [mptr 2] | | |
| 7 | ... | |
| 8 | [mptr_n] = | car_n | NULL | | |
| 9 | ||
| 10 | So set-car! does this: | |
| 11 | ||
| 12 | Before: | |
| 13 | [mptr0] = | 7 | [mptr1] | | |
| 14 | ||
| 15 | (set-car! 5 [mptr0]) | |
| 16 | ||
| 17 | After: | |
| 18 | [mptr0] = | 5 | [mptr1] | | |
| 19 | ||
| 20 | Note that we've changed the memory itself, not some amorphous "cell". We've actually manipulated memory. So now how does circular-list work? Well, let's get our same list: | |
| 21 | ||
| 22 | [mptr0] = | car0 | [mptr 1] | | |
| 23 | [mptr1] = | car1 | [mptr 2] | | |
| 24 | ... | |
| 25 | [mptr_n] = | car_n | NULL | | |
| 26 | ||
| 27 | Now xs here is [mptr0] when we get into circular-list. Then final-pair gives us back [mptr_n], and we change the cdr of it, yielding the following piece of arrangement: | |
| 28 | ||
| 29 | [mptr0] = | car0 | [mptr 1] | | |
| 30 | [mptr1] = | car1 | [mptr 2] | | |
| 31 | ... | |
| 32 | [mptr_n] = | car_n | [mptr0] | | |
| 33 | ||
| 34 | Now any time we look for the car of [mptr_n] in memory, we get the original memory address of [mptr0], and end up back at that piece of the list. Following pointers again sends us into a loop forever. | |
| 35 | ||
| 36 | eq? does magical things to understand this. |