Subject: Re: beginner question
From: Erik Naggum <>
Date: 1998/04/24
Newsgroups: comp.lang.lisp
Message-ID: <>

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

  I had the same question a few years ago, related to what SETQ at
  top-level would do to an undeclared symbol: CMUCL automatically declares
  it special, Allegro CL does not, so I thought there was something to a
  non-special global myself.  as it turns out, that's just an ordinary
  symbol, but that is mainly an artifact of the top-level loop.  I'm not
  sure I _fully_ understand Barry's and Kelly's responses, though.

| In Common Lisp, how do you define a global variable that's *not* dynamic,
| that is, that has lexical scope?

  you don't, but there is nothing to bar you from accessing a symbol's
  slots directly, such as with SYMBOL-VALUE.  since the compiler will barf
  on references to undeclared symbols, you need to access them explicitly
  not pretty, and might even be seen as thwarting the semantics of the

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

  well, in sharp contrast to Scheme, we have very easy access to lexical
  closures in Common Lisp.  e.g.,

(let ((line-counter 0))
  (defun foo (...)
    ... line-counter ...)
  (defun bar (...)
    ... line-counter ...))

  will create two functions FOO and BAR that share the lexical binding of
  LINE-COUNTER.  in Scheme, as I'm sure you are aware, the LET-form would
  remove the top-level-ness of the now _internal_ defining forms and just
  return some arbitrary values instead of defining new functions.  in this
  regard, I find Common Lisp to be far superior to Scheme, which encourages
  a proliferation of global symbols with lexical semantics as opposed to
  carefully constrained access to shared lexical bindings, which I found
  real cumbersome to do last time I tried it in Scheme, but I'm certainly
  no fan of Scheme, so I might well have missed something important.

  Abort, Retry, or Upgrade?