[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Lexically vs. Dynamically scoped Lisps.

  There are some clear advantages to lexical scoping. To illustrate this,
let's assume for a moment that Pascal has dynamic scoping. Consider the
following piece of program:

  var t : integer;
  t := 1;
  procedure P;
     ... t ...

  The person who wrote this program intended that the t referenced from
within procedure P be the one declared just above. However, since the
language has dynamic scoping, if t is redeclared prior to calling P:

  var t : integer;
  t := 5;

  then that instance of t will be used in P. This is very annoying,
since the original writer probably didn't intend this effect.
  Another clear advantage of lexical scoping is compilation. Let's use
the same piece of program. If the language is lexically scoped, then
the reference to t in P can be translated (at compile time) to a
particular address (the t declared just before P) regardless of the
value of t in effect when a call to P is made.
  If the language was dynamically scoped, then the reference to t
in P can not be translated into one address, since the t used will be
the one visible at the point P is called. Therefore, if you have several
calls to P with several redeclarations of t, you will be, in effect,
using different values of t at different calls to P.
  I hope this clarifies your question.