Re: Passing Global Variables Urgh.



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


.



Relevant Pages

  • 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) ...
    (comp.lang.lisp)
  • Passing Global Variables Urgh.
    ... I know I am not supposed to use global variables in Lisp, ... CM> (defun test2 (x) ...
    (comp.lang.lisp)
  • Re: An ode to Erlang
    ... >> Why you think lightweight threads would be hard to ... >> implement this for Lisp is beyond my imagination. ... Erlang has processes. ... > and will most likely make access to global variables slower. ...
    (comp.lang.lisp)
  • Re: SETF and variable issues in Self Similar program.
    ... recursive calls to append, and the incremented global variables, lack ... calls to functions not standardized across lisp implementations, ... I played around with C and some other programming languages when I was ... gave them up, busy with other studies, and it wasn't until I found ...
    (comp.lang.lisp)