Re: How to make a copy of a list



On Apr 29, 2:54 pm, "John Thingstad" <jpth...@xxxxxxxxx> wrote:
(defun deep-copy-sequence (sequence)
   "Recursivly copy all array's and list's."
   (cond
    ((arrayp sequence)
(setf sequence (copy-seq sequence))

ARRAYP matches multidimensional arrays, which are not sequences and
cannot be passed to COPY-SEQ. But it wouldn't be hard to add support
for copying them.

     (loop for element across sequence
           when (subtypep (type-of element) 'sequence)
           do (setf element (deep-copy-sequence element))))
    ((listp sequence)

Since all your COND tests are for type, TYPECASE can be used instead:

(typecase sequence
(vector ...)
(list ...)
...
(otherwise ...))


     (setf sequence (copy-list sequence))
     (loop for element in sequence
           when (subtypep (type-of element) 'sequence)
           do (setf element (deep-copy-sequence element)))))
   sequence)

The problem here is that although the semantics of COPY-TREE are
extended here to cover vector structure as well as cons structure,
nothing is done about atoms at all; they are not replicated.

(defgeneric deep-copy (whatever))

(defmethod deep-copy ((vec vector))
(map 'vector #'deep-copy vec))

(defmethod deep-copy ((cons cons))
(cons (deep-copy (car cons))
(deep-copy (cdr cons))))

;; Note: this is what handles (deep-copy nil) -> nil,
;; if there is no specialization to NULL, since NIL
;; is a kind of symbol.
(defmethod deep-copy ((sym symbol)) sym)

(defmethod deep-copy ((num number)) num)

Now we can add multi-dimensional array support. CLOS should match on
VECTOR more specifically if the array is one dimensional, and pass the
greater than rank 2 ones to this specialization:

(defmethod deep-copy ((array array))
;; exercise for reader
)

We don't have a T specialization, so DEEP-COPY will blow up with a no
applicable method error if it doesn't know how to copy something. E.g.

(deep-copy *random-state*)

For this one, we can just write

(defmethod deep-copy ((rs random-state))
(make-random-state rs))

Etc.



.



Relevant Pages

  • Re: How to make a copy of a list
    ...     ... (setf sequence (copy-seq sequence)) ... (defmethod deep-copy ((vec vector)) ...
    (comp.lang.lisp)
  • Re: Did I deserve this zero?
    ... Concentrated values in the black suits, comfortable rebid, two unstopped ... Given this sequence,  yes, you deserve the bottom. ... hand and trying to figure out how to bid this hand, ...   Instead you chose a bid that absolutely ...
    (rec.games.bridge)
  • Re: Balancing bid
    ...   H: AKQxxxx ... bid 2S over 2C and then bid 4H. ... reasonable on a stiff spade and AX of diamonds. ... I like this sequence best. ...
    (rec.games.bridge)
  • Re: Query Help
    ... CREATE SEQUENCE MYVALUE INCREMENT BY 1 START WITH 1; ...   MYVALUE.NEXTVAL MYVALUE ... The above incremented the MYVALUE sequence by 1 and returned the value ... ALTER SESSION SET EVENTS '10046 TRACE NAME CONTEXT FOREVER, ...
    (comp.databases.oracle.misc)
  • Re: Oracle sequence returning duplicate key for a given key name
    ... Stored procedure uses sequences to return new key. ...   declare ... procedure return duplicate key for a given key name. ... object_type = 'SEQUENCE'; ...
    (comp.databases.oracle.server)