Subject: [clisp-list] Q: why changed?

From: bruce <[email-masked]>
Date: Sat, 25 Nov 2000 21:48:47 +0800

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 Steingold <[email-masked]>
Date: 25 Nov 2000 09:27:48 -0500

> * In message <[email-masked]>
> * On the subject of "[clisp-list] Q: why changed?"
> * Sent on Sat, 25 Nov 2000 21:48:47 +0800
> * Honorable bruce <[email-masked]> writes:
>

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. 

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. 

> data multiple times and change the changes of each action.  Can anybody

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 <[email-masked]>
Date: Sun, 26 Nov 2000 09:50:55 +0800

> sense that you have to explicitly copy it when you want a copy to be

So generally speaking, one has to explicitly copy things that look like an
address such as one can use `setf' to modify its content.

> 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".