From: Tim Groner
Subject: simple programming problem correction
Date: 
Message-ID: <umgroner.877488955@castor>
In my original question, I simpified the problem too
much.  Here's what I meant to ask. (I've numbered the
lines for my convenience)  If line 3 had read
(setf (nth 2 y) 100), line 4 actually appends
different lists for x and y (which is what I want),
but when I put the function 'second' onto line 3
as well, x is updated too.


1:  (let ((x '((1 2) (2 3) (1 4))) (y))
2:    (setq y (copy-list x))
3:    (setf (second (nth 2 y)) 100)
4:    (append (list x) (list y)))

(((1 2) (2 3) (1 100)) ((1 2) (2 3) (1 100)))


I was hoping for:
(((1 2) (2 3) (1 4)) ((1 2) (2 3) (1 100)))


How can I copy the list from x into y and then
update any element in a list of a list of a
list, etc.... without altering the other list?
-- 
To reply, remove '.s.p.a.m.THIS' from my e-mail address

Tim Groner
http://home.cc.umanitoba.ca/~umgroner

From: William Paul Vrotney
Subject: Re: simple programming problem correction
Date: 
Message-ID: <vrotneyEIG6xC.777@netcom.com>
In article <··················@castor> Tim Groner
<········@cc.s.p.a.m.THIS.umanitoba.ca> writes:

>
>   In my original question, I simpified the problem too
>   much.  Here's what I meant to ask. (I've numbered the
>   lines for my convenience)  If line 3 had read
>   (setf (nth 2 y) 100), line 4 actually appends
>   different lists for x and y (which is what I want),
>   but when I put the function 'second' onto line 3
>   as well, x is updated too.
>
>
>   1:  (let ((x '((1 2) (2 3) (1 4))) (y))
>   2:    (setq y (copy-list x))
>   3:    (setf (second (nth 2 y)) 100)
>   4:    (append (list x) (list y)))
>
>   (((1 2) (2 3) (1 100)) ((1 2) (2 3) (1 100)))
>
>
>   I was hoping for:
>   (((1 2) (2 3) (1 4)) ((1 2) (2 3) (1 100)))
>
>
>   How can I copy the list from x into y and then
>   update any element in a list of a list of a
>   list, etc.... without altering the other list?

Hint: What's the difference between a linear list and a tree?

-- 

William P. Vrotney - ·······@netcom.com
From: ·····@orsi.com
Subject: Re: simple programming problem correction
Date: 
Message-ID: <877547268.29022@dejanews.com>
In article <··················@castor>,
  Tim Groner <········@cc.s.p.a.m.THIS.umanitoba.ca> wrote:
>
>
> In my original question, I simpified the problem too
> much.  Here's what I [description snipped]>
> 1:  (let ((x '((1 2) (2 3) (1 4))) (y))
> 2:    (setq y (copy-list x))
> 3:    (setf (second (nth 2 y)) 100)
> 4:    (append (list x) (list y)))
>

According to my LISP manual (and I am assuming that this is within
standards)

Only the top-level structure is copied so any lower levels are eq to
those of the original.	Thus your assumption that some pointers are still
shared is in fact true.  The Common LISP Hyperspec (whose URL I forgot at
the moment, but it's easy to find via most search engines) goes into some
detail on this. There is also a copy-tree procedure that is part of the
standard that probably does what you want.

;; example 1 - works but only with toplevel elements
(let ((x (list (list 1 2)  2 3)) y)
   (setq y (copy-list x))
   (setf (car x) 100) ;; y not affected
   y)


;; example 2 - similar to what you tried.
(let ((x (list (list 1 2)  2 3)) y)
   (setq y (copy-list x))
   (setf (caar x) 100) ;; y affected
   y)

;; example 3 - probably what you want.
(let* ((x (list (list 1 2 3) (list 4 5 6)))
       y)
   (setq y (copy-tree x))
   (setf (first (first x)) 100)
   (list x y))
result:
(((100 2 3) (4 5 6)) ((1 2 3) (4 5 6)))

Happy Coding.

Hope this helps.

-------------------==== Posted via Deja News ====-----------------------
      http://www.dejanews.com/     Search, Read, Post to Usenet