Common Lisp's copy-tree: Which objects will be referenced in common by the original and the copy?

為{幸葍}努か 提交于 2019-12-02 01:05:15

问题


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.


回答1:


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.




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/12716371/common-lisps-copy-tree-which-objects-will-be-referenced-in-common-by-the-origi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!