3

I'm reading Practical Common Lisp, and have a question about Lisp's COPY-TREE function.

The book gives the example of calling

(copy-tree '( '(1 2) '(3 4) '(5 6)))

After explaining it, the book makes this statement:

Where a cons cell in the original referenced an atomic value, the corresponding cons cell in the copy will reference the same value. Thus, the only objects referenced in common by the original tree and the copy produced by COPY-TREE are the numbers 5, 6, and the symbol NIL.

But that doesn't make sense to me. I thought all atoms would be shared between the original and the new. Therefore, I expected that 1, 2, 3, 4, 5, 6 and NIL would all be shared between the original and the copy, and that the only "new objects" would be all the CONS cells.

Which one is correct, and why?

Thanks.

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
Charlie Flowers
  • 17,338
  • 10
  • 71
  • 88

3 Answers3

5

I check the web-version, a pdf version and the hard cover. The first two are wrong as you state. The hard cover states this (bold emphasis is mine):

Where a cons cell in the original referenced an atomic value, the corresponding cons cell in the copy will reference the same value. Thus, the only objects referenced in common by the original tree and the copy produced by COPY-TREE are the numbers 1-6, and the symbol NIL.

So the hard cover book is correct.

arvid
  • 88
  • 4
  • 1
    Surprising, though ... you would think the hardcover text would be the oldest version of the 3. Which would lead one to believe that it originally said "1-6 and NIL", and later someone intentionally changed it to "5, 6 and NIL". (Either way, I can at least feel confident there's no mystery about copy-tree that I was failing to grasp). – Charlie Flowers Oct 05 '12 at 13:06
4

It is slightly more complicated.

The cons cells will be copied. Typically the objects the cons cells references will not be copied.

But there is one exception. Data like fixnums and characters can be stored inline in cons cells (and structure slots, class slots, arrays). Such data types are not necessarily EQ. That's why there is EQL.

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
  • 1
    That makes sense, but I still don't see why he draws a distinction between "5, 6 and NIL" versus "1, 2, 3 and 4". And also, what would be shared between the original and copy if the atoms *were* more complex objects that do not qualify to be stored inline? – Charlie Flowers Oct 03 '12 at 21:56
  • @Charlie Flowers: **all** cons cells will be copied. All non-primitive data will be referenced and not copied. To me it looks like Peter *tried* to explain the difference between `COPY-LIST` and `COPY-TREE`. – Rainer Joswig Oct 03 '12 at 22:36
  • 1
    Assuming I have it right now, I'd say he *did* explain the difference correctly, but then he threw in an incorrect statement about 5, 6 and NIL that created confusion about his explanation. – Charlie Flowers Oct 03 '12 at 23:12
1

The description is correct, the example is not. copy-tree would return the 1, 2 and 3 as is, copying only the cons cells.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836