setq Question.

8 views
Skip to first unread message

Nick V. Pakoulin

unread,
Feb 29, 2000, 3:00:00 AM2/29/00
to

Let's consider that the following form
(setq a 10)

is the first mention of `a' as variable. Will the binding created by setq
become dynamic or lexical? Does it depend on whether it is top-level form or
not?

I've looked about `setq' in the standard but there is nothing about it.
I tried CLISP and it looks like it makes variables in this case to be dynamic.

(let (b) (setq a 10) (setq b 20))
==> 20
a
==> 10
b
*** - EVAL: variable B has no value

TIA, Nick.

Barry Margolin

unread,
Feb 29, 2000, 3:00:00 AM2/29/00
to
In article <scm1z5w...@hookah.kazbek.ispras.ru>,

Nick V. Pakoulin <np...@kazbek.ispras.ru> wrote:
>
>Let's consider that the following form
>(setq a 10)
>
>is the first mention of `a' as variable. Will the binding created by setq
>become dynamic or lexical? Does it depend on whether it is top-level form or
>not?

The standard is silent on this, and implementations differ somewhat.
AFAIK, they all create a dynamic variable, but the extent varies: most
treat it as if the enclosing top-level form were surrounded with

(locally
(declare (special a))
...)

i.e. it's special within that form, but at least one implementation treats
it as if the form had been preceded with:

(declaim (special a))

i.e. it becomes special from then on.

--
Barry Margolin, bar...@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

Andrew Cooke

unread,
Feb 29, 2000, 3:00:00 AM2/29/00
to
You want to look at section 3.1.2.1.1 in the standard. My own rule of
thumb, which is probably a gross simplification (and possibly plain
wrong) is that unless a variable already has a lexical binding, setq
makes a dynamic binding. But read the spec.

For example:

(setq f (let ((x 5)) (defun (y) (progn (pprint x) (setq x y))))
(funcall f 3)
5
(funcall f 4)
3

Andrew
http://www.andrewcooke.free-online.co.uk/index.html


In article <scm1z5w...@hookah.kazbek.ispras.ru>,


np...@kazbek.ispras.ru (Nick V. Pakoulin) wrote:
>
> Let's consider that the following form
> (setq a 10)
>
> is the first mention of `a' as variable. Will the binding created by
setq
> become dynamic or lexical? Does it depend on whether it is top-level
form or
> not?
>

> I've looked about `setq' in the standard but there is nothing about
it.
> I tried CLISP and it looks like it makes variables in this case to be
dynamic.
>
> (let (b) (setq a 10) (setq b 20))
> ==> 20
> a
> ==> 10
> b
> *** - EVAL: variable B has no value
>
> TIA, Nick.
>


Sent via Deja.com http://www.deja.com/
Before you buy.

Erik Naggum

unread,
Feb 29, 2000, 3:00:00 AM2/29/00
to
* np...@kazbek.ispras.ru (Nick V. Pakoulin)

| Will the binding created by setq become dynamic or lexical?

setq doesn't create bindings, it modifies bindings. the binding modified
by a top-level setq is the symbol-value slot of the symbol. if this
symbol was previously unbound (i.e., it had no value), you might be
confused about the resulting behavior into believing that something
happened other than merely setting the symbols-value slot of the symbol,
but nothing else actually happened.

| I've looked about `setq' in the standard but there is nothing about it.
| I tried CLISP and it looks like it makes variables in this case to be
| dynamic.

no, you're confusing an unbound variable with the creation of a binding.

we've recently been through this, as well as several times in the past.
some will tell you that this is a major big deal with Common Lisp --
don't listen to them. just note that the semantics of a top-level setq
is well-defined, but confusing to people who don't realize that setting a
free variable, whether the symbol is declared special or not, is
operationally identical to setting the symbol-value slot of the symbol,
and setting a lexical variable is not affecting the symbol-value slot of
any symbol.

the ultimate advice, however: it is not a good idea to use free variables
without declaring them special or knowing _exactly_ what's going on.

#:Erik

Nick V. Pakoulin

unread,
Mar 2, 2000, 3:00:00 AM3/2/00
to
Erik Naggum <er...@naggum.no> writes:

> * np...@kazbek.ispras.ru (Nick V. Pakoulin)
> | Will the binding created by setq become dynamic or lexical?
>
> setq doesn't create bindings, it modifies bindings. the binding modified
> by a top-level setq is the symbol-value slot of the symbol. if this
> symbol was previously unbound (i.e., it had no value), you might be
> confused about the resulting behavior into believing that something
> happened other than merely setting the symbols-value slot of the symbol,
> but nothing else actually happened.

May be I don't understand something fundamentally simple, but what about this
piece of code (CLISP):

(values (boundp 'bazz) (setq bazz 10) (boundp 'bazz))
NIL ;
10 ;
T

>
> | I've looked about `setq' in the standard but there is nothing about it.
> | I tried CLISP and it looks like it makes variables in this case to be
> | dynamic.
>
> no, you're confusing an unbound variable with the creation of a binding.
>
> we've recently been through this, as well as several times in the past.
> some will tell you that this is a major big deal with Common Lisp --
> don't listen to them. just note that the semantics of a top-level setq
> is well-defined, but confusing to people who don't realize that setting a
> free variable, whether the symbol is declared special or not, is
> operationally identical to setting the symbol-value slot of the symbol,
> and setting a lexical variable is not affecting the symbol-value slot of
> any symbol.
>
> the ultimate advice, however: it is not a good idea to use free variables
> without declaring them special or knowing _exactly_ what's going on.
>
> #:Erik

Nick.

Erik Naggum

unread,
Mar 2, 2000, 3:00:00 AM3/2/00
to
* np...@kazbek.ispras.ru (Nick V. Pakoulin)
| May be I don't understand something fundamentally simple, but what about this
| piece of code (CLISP):

yeah, what about it?

the reason I *hate* examples is that they communicate exactly _nothing_
in the vast majority of cases. the set of possible questions you are
asking is virtually unbounded.

I assume you are confused about what the function boundp returns, but at
what point your understanding is solid is inherently uknowable with just
your example to go on, and any good answer would have to start off from
where you last had a reasonably solid understanding of the issues. don't
assume that people have time to figure out what you're trying to ask them
about.

please try to rephrase your questions, and don't use a single line of
code as an example -- nothing good ever comes from stuffing things into a
Lisp system and looking confused at the answer, anyway. either you know
what you're doing, or you set out to learn. chances are then very good
you will have answered your own question in the process of asking it well.

#:Erik

Reply all
Reply to author
Forward
0 new messages