Re: How to make a copy of a list



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.
.



Relevant Pages

  • Re: A couple isolated examples of "devolution"
    ... > I used to waste quite a bit of time "surfing" a couple of tree of life ... It is as if Evolution (i.e. this universe as a patterning process - ... condensed into atoms of hydrogen that gravity then clumped together into ... be taken as my warning against making too clear cut a distinction ...
    (sci.bio.evolution)
  • Re: sub_atom implementation
    ... I am using SWI Prolog. ... I have two issues regarding sub_atom/5 predicate. ... atoms are shared between threads. ... manipulating lists of characterusing ...
    (comp.lang.prolog)
  • Re: String manipulation in Prolog
    ... Normally a string is a list of character-codes. ... >> as a chunk of characters you can use atoms. ... Strings (lists) are very different. ... They are great for working at the character level. ...
    (comp.lang.prolog)
  • How can "cons per call" be so different for these two very similar functions?
    ... I've written two functions to compare two lists of string tokens. ... (defun atoms-in-common (atoms corpus) ... strings), return a list of those atoms ...
    (comp.lang.lisp)
  • Re: Newbie: problem understanding a lat and an atom (The Little Schemer)
    ... Or symbols and numbers might be atoms. ... But non-empty lists are definitely not atoms. ... Scheme reports do not define first and rest. ... They define car and cdr. ...
    (comp.lang.scheme)