Re: Passing Global Variables Urgh.



In article <1188430108.062743.252690@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
landspeedrecord <landspeedrecord@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"))

This sets the local variable X.

TEST2

CM> (test2 *x*)
"orrgoaarghhh"

Above is the same as calling

(test2 "blargh")


CM> *x*
"blargh"

My only guess is that the function "test2" gets passed a copy of the
global variable

No, it gets passed a value, not a 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?

But you have to use their name if you want to access them.

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

Evaluation of a function call (test2 *x*)

1) we see that TEST2 is a function.
2) a function call evaluates all the arguments
3) evaluation of *X*
4) *X* is "blargh"
5) call TEST2 with "blargh" as the argument for X
...
....

So Lisp does not pass variables, but values.
.



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