Re: How to make a copy of a list
- From: Trastabuga <lispercat@xxxxxxxxx>
- Date: Tue, 29 Apr 2008 16:46:13 -0700 (PDT)
On Apr 29, 5:54 pm, "John Thingstad" <jpth...@xxxxxxxxx> wrote:
På Tue, 29 Apr 2008 23:24:00 +0200, skrev John Thingstad
<jpth...@xxxxxxxxx>:
(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)
Third and final version.. Trades succinctness for efficiency.
Lisp needs a efficient generic iterator over a sequence..
(Looking at the macroexpansion of iter it was just length and elt under
the hood)
(defun deep-copy-sequence (sequence)
"Recursivly copy all array's and list's."
(cond
((arrayp sequence)
(setf sequence (copy-seq sequence))
(loop for element across sequence
when (subtypep (type-of element) 'sequence)
do (setf element (deep-copy-sequence element))))
((listp sequence)
(setf sequence (copy-list sequence))
(loop for element in sequence
when (subtypep (type-of element) 'sequence)
do (setf element (deep-copy-sequence element)))))
sequence)
--------------
John Thingstad
John, your original version of deep-copy-sequence works, but the
latest doesn't (the original list gets changed).
I can't put my finger on it, the simple copy-list should work, but the
tree is so complicated so I can't make a simple test and show why the
original list gets mutilated.
Thank you!
Andrew
.
- Follow-Ups:
- Re: How to make a copy of a list
- From: Kaz Kylheku
- 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
- Re: How to make a copy of a list
- From: John Thingstad
- Re: How to make a copy of a list
- From: John Thingstad
- How to make a copy of a list
- Prev by Date: Re: Request for comments on LISP-newbie style
- Next by Date: Re: Scope Question
- 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
|