How to make a new list elsewhere, Lisp

The name is self-evident. How can I build a new list X from another list Y (of the same structure), but as a result, a list indicating somewhere else in the memory area is practically a different object? I tried with make-list :initial-element Yor added to the empty list, but I still get the same object. Thanks!

+3
source share
3 answers

Generic Lisp

Use the COPY LIST to copy the first level of the list.

Use COPY-TREE to copy the conses tree, a tiered list will be copied at all levels.

COPY-TREE, COPY-LIST conses . ( EQL, EQ).

. Lisp HyperSpec Conses .

+12

, COPY-LIST.

+3

My copy worked (using sbcl).

REPL:

(defvar a '((1 2 3) (4 5 6)))

(defvar b (copy-tree a))

(setf (nth 0 (nth 0 b)) "4")

a

; ((1 2 3) (4 5 6)) is returned

b

;(("4" 2 3) (4 5 6)) is returned

Perhaps if you provided us with more sources, we could help.

0
source

Source: https://habr.com/ru/post/1722861/


All Articles