Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- A list in lisp looks like this:
- List = [mptr 0]
- [mptr0] = | car0 | [mptr 1] |
- [mptr1] = | car1 | [mptr 2] |
- ...
- [mptr_n] = | car_n | NULL |
- So set-car! does this:
- Before:
- [mptr0] = | 7 | [mptr1] |
- (set-car! 5 [mptr0])
- After:
- [mptr0] = | 5 | [mptr1] |
- 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:
- [mptr0] = | car0 | [mptr 1] |
- [mptr1] = | car1 | [mptr 2] |
- ...
- [mptr_n] = | car_n | NULL |
- 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:
- [mptr0] = | car0 | [mptr 1] |
- [mptr1] = | car1 | [mptr 2] |
- ...
- [mptr_n] = | car_n | [mptr0] |
- 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.
- eq? does magical things to understand this.
Add Comment
Please, Sign In to add comment