Re: How to make a copy of a list
- From: "John Thingstad" <jpthing@xxxxxxxxx>
- Date: Tue, 29 Apr 2008 23:54:09 +0200
På Tue, 29 Apr 2008 23:24:00 +0200, skrev John Thingstad <jpthing@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
.
- Follow-Ups:
- Re: How to make a copy of a list
- From: Trastabuga
- 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
- How to make a copy of a list
- Prev by Date: Re: How to make a copy of a list
- Next by Date: 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
|