From: rmagere
Subject: Problem with global variables [i.e. showing my ignorance]
Date:
Message-ID: <bvl8c7$g4o$1@news.ox.ac.uk>
Hi,
the problem I have is that I want to have a global variable holding some
information about actions that have to be taken at certain times, that
information needs to get changed with time until when it gets re-initialized
to what its original values where.
Here is an example:
(defvar *trials* nil)
(defclass trial ()
((name :accessor name :initarg :name :initform nil)
(data :accessor data :initarg :data :initform nil)))
(defun trials ()
(list (list (make-instance 'trial :name 'trial-0 :data '(0 1 2))
(make-instance 'trial :name 'trial-1 :data '(2 3 4)))
(list (make-instance 'trial :name 'trial-0 :data '(a b c))
(make-instance 'trial :name 'trial-1 :data '(d e f)))))
(setf *trials* (trials))
(defun initialize-trials ()
(setf *trials* (trials)))
So when I call (first (data (first (first *trials*)))) I get 0, then if I
setf it to 111 calling it again gives me 111 as expected. However if I then
run (initialize-trials) and then call (first ....) I still get 111 rather
than 0 as I (obviously erroneously) expected.
Can anyone highlight what I have done wrong and how to around the problem?
Is it that (defun initialize-trials) creates another level and the *trials*
in it isn't the same as the *trials* visible on the outside?
Thanks for any help,
rmagere
From: Christophe Rhodes
Subject: Re: Problem with global variables [i.e. showing my ignorance]
Date:
Message-ID: <squ129hj4e.fsf@lambda.dyndns.org>
"rmagere" <·······@*the-mail-that-burns*.com> writes:
> (defun trials ()
> (list (list (make-instance 'trial :name 'trial-0 :data '(0 1 2))
^
literal data ----------------------------'
> So when I call (first (data (first (first *trials*)))) I get 0, then if I
> setf it to 111 calling it again gives me 111 as expected. However if I then
> run (initialize-trials) and then call (first ....) I still get 111 rather
> than 0 as I (obviously erroneously) expected.
>
> Can anyone highlight what I have done wrong and how to around the problem?
You're modifying literal data when you setf it to 111. This is
undefined. If you change the call to
... :data (list 0 1 2)
everything should start working.
Christophe
--
http://www-jcsu.jesus.cam.ac.uk/~csr21/ +44 1223 510 299/+44 7729 383 757
(set-pprint-dispatch 'number (lambda (s o) (declare (special b)) (format s b)))
(defvar b "~&Just another Lisp hacker~%") (pprint #36rJesusCollegeCambridge)
From: rmagere
Subject: Re: Problem with global variables [i.e. showing my ignorance]
Date:
Message-ID: <bvl9fu$gm7$1@news.ox.ac.uk>
Christophe Rhodes wrote:
> You're modifying literal data when you setf it to 111. This is
> undefined. If you change the call to
> ... :data (list 0 1 2)
> everything should start working.
Thanks everything works now - I have actually realized that I have had the
same problem sometime ago in another context and then it had dissappeard, I
guess that I must have changed to list for some reason or another without
actually understading the issue involved.
Thanks again.