Re: How to make a copy of a list
- From: Kaz Kylheku <kkylheku@xxxxxxxxx>
- Date: Tue, 29 Apr 2008 14:20:58 -0700 (PDT)
On Apr 29, 12:30 pm, Trastabuga <lisper...@xxxxxxxxx> wrote:
when copy-list or copy-tree are not deep enough?
copy-tree is deeper than copy-list.
Copy-list will copy the list structure, but use the same elements (CAR-
s) regardless of whether they are themselves conses or atoms.
Copy-tree will copy all of the conses (which are transitively
reachable by navigating CAR and CDR from the root cons). But anything
which is not a CONS is not copied.
So for instance if you have a tree full of structs, copy-tree will not
make copies of those structs; they are shared between the new tree and
old. Structs are atoms, and copy-tree copies only conses.
So the answer is that if copy-tree is not deep enough, then copy-list
is not deep enough. And copy-tree is not deep enough whenever it's
insufficient to just copy the tree structure, without actually copying
the atoms.
The predicate ``deep enough'' can only be evaluated with respect to
your program's requirements.
I have a big list and I need a function that returns one of its
branches depending on condition.
Later on, in another function I use that branch and modify it
(appending other lists to it).
To mutually isolate copies of a list from the effects of destructive
append operations done on any of the copies, copy-list is good enough.
To mutually isolate copies of a tree from any destructive tree
structure manipulation, copy-tree is good enough.
Neither of these will shield the copies from destructive modifications
of the atoms themselves.
Now, even though I return that branch using copy-tree, still when I
modify that branch, it modifies the original tree.
That's impossible, unless by ``modify'' you mean that you are changing
the contents of the atoms found at the leaves of the tree.
Is it because copy-tree only copies conses but not the atoms
themselves?
How can I do a deep copy of the list so it creates a completely new
instance of that branch?
You could perform a COPY-TREE first, followed by a recursive walk of
the newly consed tree structure, in which you replace all mutable
atoms by copies.
This is difficult to do in general, because there is no unified
operation for copying any atom. You could write a generic copy
function and specialize it to the objects you care about in your
program.
Mutable atoms would be things like vectors (and, recursively, the
mutable atoms contained in vectors therein), strings, structs, CLOS
objects. You don't bother with numbers and symbols.
.
- Follow-Ups:
- Re: How to make a copy of a list
- From: Rob Warnock
- Re: How to make a copy of a list
- References:
- How to make a copy of a list
- From: Trastabuga
- How to make a copy of a list
- Prev by Date: Re: array initialisation
- Next by Date: Re: How to make a copy of a list
- Previous by thread: Re: How to make a copy of a list
- Next by thread: Re: How to make a copy of a list
- Index(es):
Relevant Pages
|