Passing Global Variables Urgh.



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???

.



Relevant Pages

  • Re: Passing Global Variables Urgh.
    ... I know I am not supposed to use global variables in Lisp, ... CM> (defun test2 (x) ...
    (comp.lang.lisp)
  • Re: Passing Global Variables Urgh.
    ... I know I am not supposed to use global variables in Lisp, ... CM> (defun test2 (x) ... But then you change x to refer ...
    (comp.lang.lisp)
  • Re: Passing Global Variables Urgh.
    ... I know I am not supposed to use global variables in Lisp, ... CM> (defun test2 (x) ... dynamic variables ...
    (comp.lang.lisp)
  • Re: Global variables done right
    ... Lisp for the first time. ... deficiencies in the design of Common Lisp. ... three problems with CL's design when it comes to global variables: ... a conforming implementation of Common Lisp could expand DEFCONSTANT ...
    (comp.lang.lisp)
  • Re: Global variables done right
    ... design of Common Lisp. ... There are no global lexicals. ... all global variables should have named that are bookended by asterisks. ...
    (comp.lang.lisp)