From: Matthew Danish

Subject: Re: Question about scope and let...

Date: 2003-1-28 14:48

On Tue, Jan 28, 2003 at 09:03:38AM +0100, Fabrizio Morbini wrote:
> Hi, why there is this behaviour? > > CL-USER(1): (setq var-test 5) > 5
First, you should not use SETQ to create a variable. It may work in certain implementations, but its not defined according to the standard. Looking at the example below, it seems that Allegro places the binding in the global lexical environment. (By contrast, CMUCL will assume that you meant to type DEFVAR, and will make the variable a special).
> CL-USER2: (let ((var-test 4)) var-test (eval 'var-test))
Don't you mean: (let ((var-test 4)) (print var-test) (eval 'var-test)) ?
> > 4 > 5
The LET shadows the outer lexical binding, so the first value is as expected. However, EVAL does not, and cannot, operate in the current lexical environment. There is no provision for that. Consider this code fragment: (let ((code 'a)) (let ((a 1)) (eval code))) Since EVAL is a normal function, all it receives as parameter is the piece of data, which happens to be a symbol. If this code fragment were to evaluate the lexical binding A that is established, that would mean that EVAL somehow magically obtained the current lexical environment. CL does not mandate a lexical environment argument for EVAL because it prevents efficient implementation. Thus EVAL evaluates code in the ``null lexical environment'' (though in your case, Allegro has allowed you to add bindings to this ``null'' lexical environment). Basically, it boils down to: ``Don't use EVAL unless you really know what you're doing.'' -- ; Matthew Danish <andrew.cmu.edu at mdanish> ; OpenPGP public key: C24B6010 on keyring.debian.org ; Signed or encrypted mail welcome. ; "There is no dark side of the moon really; matter of fact, it's all dark."