Subject: Re: beginner question
From: rpw3@rigden.engr.sgi.com (Rob Warnock)
Date: 1998/04/24
Newsgroups: comp.lang.lisp
Message-ID: <6hp3jt$4t984@fido.asd.sgi.com>

Thomas A. Russ <tar@sevak.isi.edu> replied (to Adam Lasser):
+---------------
| Third, if you really need to have global values, then you should tell
| the compiler that you intend the variables to be globally visible and
| have dynamic scope by using a DEFVAR or DEFPARMETER declaration in your
| code.
+---------------

That brings up a stupid-beginner-question of *mine*...

In Common Lisp, how do you define a global variable that's *not* dynamic,
that is, that has lexical scope? In Scheme, of course, all variables are
lexical (unless one has done some hackery with dynamic-wind), so this is
simply:

	(define line-counter 0)

and then if you later do something like:

	(define bar (lambda () (display line-counter) (newline)))

	(define foo (lambda (...)
		      ...
		      (let loop ((line-counter 47))
			...
			(bar)
			...
			(loop (+ line-counter 1)))
		      ...))

the new definition of "line-counter" lexically shadows the global one
*within* "foo", but the call to "bar" uses the global version.

But if I understand your comment about DEFVAR & DEFPARMETER implying
dynamic scope (which agrees with my reading of CLtLx & CLHS), then
in CL "bar" would see the current *dynamic* value of "line-counter",
that is, the value established by the "let" in "foo".

How does one avoid this when one wants normal (to a Scheme user)
lexical scope for a global variable?  What declaration or initial
definition form does one use?


-Rob

-----
Rob Warnock, 7L-551		rpw3@sgi.com   http://reality.sgi.com/rpw3/
Silicon Graphics, Inc.		Phone: 650-933-1673 [New area code!]
2011 N. Shoreline Blvd.		FAX: 650-933-4392
Mountain View, CA  94043	PP-ASEL-IA