Subject: Re: distinction?
From: Erik Naggum <erik@naggum.net>
Date: 08 Dec 2000 01:45:50 +0000
Newsgroups: comp.lang.lisp
Message-ID: <3185228750011668@naggum.net>

* Xenophon Fenderson the Carbon(d)ated
| I'm pretty clueless when it comes to dynamic scope.

  At least you recognize that.  A lot of Scheme people think they know
  everything about dynamic scope when they are clueless about it.

| Why is it so useful?

  Because it embodies intrinsic language support for the protocol that
  surrounds every _sane_ use of global variables (or more precisely,
  variables with indefinite scope -- referenceable from everywhere).
  Namely, if you bind a special variable, you are guaranteed that it
  will be restored to the value it had when you entered the dynamic
  extent when you exit that scope, regardless of errors, signals, non-
  local transfers of control, anything.  Implementing this yourself is
  terribly hard and can get so low-level most programmers and language
  designers alike just give up and live with errors in restoring the old
  value.  Typically, however, you don't even _have_ binding forms for
  global variables in languages that do not support special variables,
  so you end up _having_ to use a local copy and set and restore the
  global variable yourself.

(let ((*print-base* 16))
  ...)

  is in principle functionally similar to this:

(let ((old-print-base *print-base*))
  (unwind-protect
      (progn
        (setq *print-base* 16)
        ....)
    (setq *print-base* old-print-base)))

  although the differences will be very noticeable if you have parallel
  bindings, such as you have in a multiprocessing environment, with one
  binding per thread/process of this variable.  Special variable binding
  takes care of all this.

| i doubt I understand even what dynamic scope really is.

  Well, it's a misnomer, so that's not so strange.  "Dynamic scope" is
  short-hand for "indefinite scope, dynamic extent".  That is, you can
  value from anywhere (indefinite/global scope), but that particular
  binding extends from binding establishment through disestablishment,
  which looks just like a lexical binding in that it covers only so much
  code, but because of the indefinite scope, it actually covers all code
  that gets executed between binding establishment and disestablishment.

| I grew up using Scheme, so I'm used to DEFINE creating a binding in
| the top-most environment.

  Well, this is one instance of what I consider damaging in premature
  exposure to Scheme.  It is too damn low-level and _unevolved_ as a
  language, yet it looks amazingly complex and rich through its simple
  elegance (which fools mostly any intellectual, just like simple and
  elegant and wrong political theories trap bright students) until you
  start to figure out the kind of complexity the real world needs.

| I fail to see how dynamic scope is any different from top-most-level
| bindings, although dynamic bindings established in one function can
| affect another, right?

  I hope this has helped.

#:Erik
-- 
  "When you are having a bad day and it seems like everybody is trying
   to piss you off, remember that it takes 42 muscles to produce a
   frown, but only 4 muscles to work the trigger of a good sniper rifle."
								-- Unknown