Re: How to make a copy of a list
- From: "John Thingstad" <jpthing@xxxxxxxxx>
- Date: Tue, 29 Apr 2008 23:24:00 +0200
På Tue, 29 Apr 2008 22:26:55 +0200, skrev John Thingstad <jpthing@xxxxxxxxx>:
You write your own. copy-seq works like copy-list but works on arrays as well.
sequencep sais whether it is a sequence. Since a list is also a sequence you just need a
depth first traversal to duplicate the structure.
Lot of nonsence here. (subtypep (type-of sequence) 'sequence) determines if it is a sequence.
Traversal is breath first. (Next time I will write the code FIRST.)
Something like:
(defun deep-copy-sequence (sequence)
"Recursivly copy all array's and list's."
(when (subtypep (type-of sequence) 'sequence)
(setf sequence (copy-seq sequence))
(loop for i from 0 below (length sequence) do
(setf (elt sequence i) (deep-copy-sequence (elt sequence i)))))
sequence)
Using the iterate package this is a more efficient version.
(defun deep-copy-sequence (sequence)
"Recursivly copy all array's and list's."
(when (subtypep (type-of sequence) 'sequence)
(setf sequence (copy-seq sequence))
(iter (for element in-sequence sequence)
(when (subtypep (type-of element) 'sequence)
(setf element (deep-copy-sequence element)))))
sequence)
1. No need recursivly go down the structure for atoms.
2. elt and length are not needed. Both have order n complexity for list's.
This works for characters, numbers, vectors, strings, arrays
You get in trouble with structures and classes.
They can in themself have substructure.
For your own classes define a method deep-copy.
For library classes/structure all bets are off. You could hack something together I suppose using introspection..
--------------
John Thingstad
.
- Follow-Ups:
- Re: How to make a copy of a list
- From: Thomas A. Russ
- Re: How to make a copy of a list
- From: John Thingstad
- Re: How to make a copy of a list
- References:
- How to make a copy of a list
- From: Trastabuga
- Re: How to make a copy of a list
- From: John Thingstad
- How to make a copy of a list
- Prev by Date: Re: How to make a copy of a list
- 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
|