From: bruce <bru...@16...> - 2000-11-25 13:49:53
|
Dear all, I have a program similar to the following. I spent quite a long time to debug and finally spotted that the bug is here. > (setf a-list '(1 2 3 4 5) ; a dynamic variable with global scope (1 2 3 4 5) >(defun change2 (lst el) ; a function to modify data (setf (nth 2 lst) el) lst) CHANGE2 >(let ((lst a-list)) (setf lst (change2 lst 10))) (1 2 10 4 5) >a-list (1 2 10 4 5) I thought I was modifying a local copy of the global data, but actually the global data was changed as well. I don't want to modify the global data, since I have to use the global data multiple times and change the changes of each action. Can anybody help? Thanks a lot! Yang Shouxun -- If you continually give you will continually have. |
From: Sam S. <sd...@gn...> - 2000-11-25 14:28:33
|
> * In message <3A1...@16...> > * On the subject of "[clisp-list] Q: why changed?" > * Sent on Sat, 25 Nov 2000 21:48:47 +0800 > * Honorable bruce <bru...@16...> writes: > > I have a program similar to the following. I spent quite a long time to > debug and finally spotted that the bug is here. > > > > (setf a-list '(1 2 3 4 5) ; a dynamic variable with global > scope note that `a-list' is set to a constant in the sense that you may not modify it portably (some lisps will mark the memory where this quoted list resides as immutable and signal an error when you try to modify it. > (1 2 3 4 5) > >(defun change2 (lst el) ; a function to modify data > (setf (nth 2 lst) el) > lst) > CHANGE2 > >(let ((lst a-list)) > (setf lst (change2 lst 10))) > (1 2 10 4 5) > >a-list > (1 2 10 4 5) > > > I thought I was modifying a local copy of the global data, but actually > the global data was changed as well. a-list is just a cons cell, which is a reference or a pointer or an address - call it as you like, but it is not an immediate object in the sense that you have to explicitly copy it when you want a copy to be created. > I don't want to modify the global data, since I have to use the global > data multiple times and change the changes of each action. Can anybody > help? you should copy the global list each time, like this: (let ((lst (copy-list a-list))) (setf lst (change2 lst 10))) You will probably benefit from reading "ANSI CL" by Paul Graham. You might also want to subscribe to the comp.lang.lisp newsgoup. Good luck and thanks for using CLISP! -- Sam Steingold (http://www.podval.org/~sds) Support Israel's right to defend herself! <http://www.i-charity.com/go/israel> main(a){a="main(a){a=%c%s%c;printf(a,34,a,34);}";printf(a,34,a,34);} |
From: bruce <bru...@16...> - 2000-11-26 01:51:57
|
Sam Steingold wrote: > > I thought I was modifying a local copy of the global data, but actually > > the global data was changed as well. > > a-list is just a cons cell, which is a reference or a pointer or an > address - call it as you like, but it is not an immediate object in the > sense that you have to explicitly copy it when you want a copy to be > created. > So generally speaking, one has to explicitly copy things that look like an address such as one can use `setf' to modify its content. > > > I don't want to modify the global data, since I have to use the global > > data multiple times and change the changes of each action. Can anybody > > help? > > you should copy the global list each time, like this: > > (let ((lst (copy-list a-list))) > (setf lst (change2 lst 10))) > > You will probably benefit from reading "ANSI CL" by Paul Graham. > You might also want to subscribe to the comp.lang.lisp newsgoup. Thanks for your suggestion. I could not find a copy of Graham's book for the time being. And I don't think there is any downloadable electronic copy. Thanks for your prompt reply. -- Give all orders verbally. Never write anything down that might go into a "Pearl Harbor File". |