Re: Passing Global Variables Urgh.
- From: Scott Burson <FSet.SLB@xxxxxxxxx>
- Date: Thu, 30 Aug 2007 00:17:54 -0000
On Aug 29, 4:28 pm, landspeedrecord <landspeedrec...@xxxxxxxxx> wrote:
My only guess is that the function "test2" gets passed a copy of the
global variable that is exists inside the function "test2".
Bingo. Lisp, like most modern languages, is call-by-value. The
`setq' within `test2' only modifies the value of `x'.
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???
Lisp never passes variables by reference. You must be coming from
some language (Fortran???) where call-by-reference is the default.
What you can do in Lisp (and Java, and ...) is to bind a global
variable to an object which can then be modified:
(defstruct x-holder
thing)
(defvar *x* (make-x-holder :thing "blargh"))
(defun test2 (x)
(setf (x-holder-thing x) "orrgoaarghhh"))
-- Scott
.
- References:
- Passing Global Variables Urgh.
- From: landspeedrecord
- Passing Global Variables Urgh.
- Prev by Date: Re: Passing Global Variables Urgh.
- Next by Date: Re: Simple question about directories
- Previous by thread: Re: Passing Global Variables Urgh.
- Next by thread: Re: Passing Global Variables Urgh.
- Index(es):
Relevant Pages
|