Re: Passing Global Variables Urgh.
- From: andrea <kerny404@xxxxxxxxx>
- Date: Wed, 29 Aug 2007 16:49:04 -0700
On 30 Ago, 01:28, landspeedrecord <landspeedrec...@xxxxxxxxx> wrote:
Total newbie question....
First off... I know I am not supposed to use global variables in Lisp,
however...
Can anyone explain to me why the following bit of code doesn't work?
I don't get it and I think it is because there is something very
fundamental I do not understand about Lisp.
CM> (defvar *x* "blargh")
*X*
CM> *x*
"blargh"
CM> (defun test2 (x)
(setq x "orrgoaarghhh"))
TEST2
CM> (test2 *x*)
"orrgoaarghhh"
CM> *x*
"blargh"
My only guess is that the function "test2" gets passed a copy of the
global variable that is exists inside the function "test2". But isn't
the whole point of Defvar & Defparameter to create a global variable
that can be accessed from anywhere? If a copy of a global variable is
always passed into functions what good is a global variable?
Now I could write "test2" so that it was:
(defun test2 ()
(setq *x* "orrgoaarghhh"))
but that isn't the same as passing the global variable.
I just don't get it. Urgh???
Interesting, I don't know but I think it's because defvar creates
dynamic variables (from hyperspec)
dynamic variable n. a variable the binding for which is in the dynamic
environment. See special.
that maybe behaves in this strange way..
.
- References:
- Passing Global Variables Urgh.
- From: landspeedrecord
- Passing Global Variables Urgh.
- Prev by Date: Re: Passing Global Variables Urgh.
- Next by Date: Re: Passing Global Variables Urgh.
- Previous by thread: Re: Passing Global Variables Urgh.
- Next by thread: Re: Passing Global Variables Urgh.
- Index(es):
Relevant Pages
|
|