Re: Passing Global Variables Urgh.
- From: Rainer Joswig <joswig@xxxxxxx>
- Date: Thu, 30 Aug 2007 01:45:48 +0200
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.
.
- References:
- Passing Global Variables Urgh.
- From: landspeedrecord
- Passing Global Variables Urgh.
- Prev by Date: Passing Global Variables Urgh.
- Next by Date: Re: Passing Global Variables Urgh.
- Previous by thread: Passing Global Variables Urgh.
- Next by thread: Re: Passing Global Variables Urgh.
- Index(es):
Relevant Pages
|