From: Mark Watson
Subject: Re: Using setf on a List of Lists
Date: 
Message-ID: <B939D00D.E1A%markw@markwatson.com>
Hello Donovan,

When you built the list L, you did it by sharing a
single instance of the list (0 0 0 0) 3 times. If you
change the shared sub-list, then you see the change appear 3 times.

An example will hopefully make this clear:

> (setq x '(0 0 0 0))
(0 0 0 0)
> (setq y (list x x x))
((0 0 0 0) (0 0 0 0) (0 0 0 0))
> (setf (nth 2 (nth 1 y)) 'x)
X
> x
(0 0 X 0)
> y
((0 0 X 0) (0 0 X 0) (0 0 X 0))
> (setq z '((0 0 0 0) (0 0 0 0) (0 0 0 0)))
((0 0 0 0) (0 0 0 0) (0 0 0 0))
> (setf (nth 2 (nth 1 z)) 'x)
X
> z
((0 0 0 0) (0 0 X 0) (0 0 0 0))
> 

When we built y, we used the same shared sublist x; changing x affects all
three references to x in y.

However, we built z with 3 unique sublists; that is, each sublist in z
refers to a separate list in memory and not a shared sublist like in y.

-Mark

-- Mark Watson, author and consultant
-- Open Content and Open Source: www.markwatson.com
-- Commercial AI software: www.knowledgebooks.com