From: Robert Goldman
Subject: question: list idiosyncracy problem
Date:
Message-ID: <3651BB43.1FFDB531@mail.com>
Hi
While trying to do some simple list operations I'm finding some
idiosyncracies that I am unable to
resolve. Maybe someone has an explanation and/or solution. I am using
MCL 4.2 (Macintosh
Common Lisp). I've reduced the example to the bare essentials in order
to focus on the specific
problem (the context is a simple genetic algorithm). Any help would be
greatly appreciated.
- This is what I am trying to do:
1) start with a list of integers (which are randomly generated in the
original source code) of 'seq-length' length, say:
(setf seq-length 4)
current-seq => (61 60 65 70)
2) copy above sequence 'seq-length' times to another sequence:
working-data => ((61 60 65 70)(61 60 65 70)(61 60 65 70)(61 60 65 70))
3) modify (gets mutated by +/- 1 in original code) the 'i'th element
(inner list) of the 'i'th list (outer list), for simplicity let's say
to 99:
working-data=> ((99 60 65 70)(61 99 65 70)(61 60 99 70)(61 60 65 99))
- OK, here's the problem I'm encountering:
I've tried various methods of implementing step 2 above such as:
A (fill working-data current-seq)
or
B (dotimes (i seq-length)
(setf (nth i working-data) current-seq))
which all 'seem' to work in creating a list as specified in step 2 (when
printed)
working-data => ((61 60 65 70)(61 60 65 70)(61 60 65 70)(61 60 65 70))
but when I run them through my step 3 code below:
(dotimes (i seq-length)
(setf (nth i (nth i working-data)) 99))
they produce an anamoly:
working-data=> ((99 99 99 99) (99 99 99 99) (99 99 99 99) (99 99 99 99))
THE STRANGE THING IS THIS, when I replace step the step 2 solutions (A
or B) with a straighforward assignment:
(setf working-data '((61 60 65 70) (61 60 65 70) (61 60 65 70) (61 60 65
70))
and then run it through the step 3 code, it creates the desired
modification:
working-data=> ((99 60 65 70) (61 99 65 70) (61 60 99 70) (61 60 65
99)))
I know this was quite a long explanation butI hope it was reasonably
clear, any ideas?
Thanks, Robert
In article <·················@mail.com>,
Robert Goldman <········@mail.com> wrote:
>B (dotimes (i seq-length)
> (setf (nth i working-data) current-seq))
Try:
(dotimes (i seq-length)
(setf (nth i working-data) (copy-seq current-seq)))
I originally included a short explanation, but I'll leave that as an
exercise for the readers (which is probably futile, since I'll bet someone
else will respond with a detailed explanation).
--
Barry Margolin, ······@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Don't bother cc'ing followups to me.
Barry Margolin <······@bbnplanet.com> writes:
> I originally included a short explanation, but I'll leave that as an
> exercise for the readers (which is probably futile, since I'll bet someone
> else will respond with a detailed explanation).
Now I won't ;)
--
Pierre Mai <····@acm.org> http://home.pages.de/~trillian/
"One smaller motivation which, in part, stems from altruism is Microsoft-
bashing." [Microsoft memo, see http://www.opensource.org/halloween1.html]
Robert Goldman <········@mail.com> writes:
< - This is what I am trying to do:
<
< 1) start with a list of integers (which are randomly generated in the
< original source code) of 'seq-length' length, say:
<
< (setf seq-length 4)
<
< current-seq => (61 60 65 70)
<
< 2) copy above sequence 'seq-length' times to another sequence:
<
< working-data => ((61 60 65 70)(61 60 65 70)(61 60 65 70)(61 60 65 70))
<
< 3) modify (gets mutated by +/- 1 in original code) the 'i'th element
< (inner list) of the 'i'th list (outer list), for simplicity let's say
< to 99:
<
< working-data=> ((99 60 65 70)(61 99 65 70)(61 60 99 70)(61 60 65 99))
<
<
< - OK, here's the problem I'm encountering:
<
< I've tried various methods of implementing step 2 above such as:
<
< A (fill working-data current-seq)
<
< or
<
< B (dotimes (i seq-length)
< (setf (nth i working-data) current-seq))
<
< which all 'seem' to work in creating a list as specified in step 2 (when
< printed)
Try doing a (setq *print-circle* t); then the sequences will print
showing you the shared elements. Some trivia follows.
Another way to collect items into a list is with loop.
(setq *print-circle* t) => t
(loop repeat 4 collect '(61 60 65 70)) => (#1=(61 60 65 70) #1# #1# #1#)
You could do
(loop repeat 4 collect (copy-seq '(61 60 65 70)))
=> ((61 60 65 70) (61 60 65 70) (61 60 65 70) (61 60 65 70))
. Have fun...