Subject: Re: setq vs setf?
From: (Rob Warnock)
Date: Sat, 28 Jan 2006 20:29:46 -0600
Newsgroups: comp.lang.lisp
Message-ID: <>
Jeff M. <> wrote a good explanation except for one point:
| SETQ is the simple method of binding a symbol to a value.
| Keep in mind, that's all it does. It binds a symbol to a value:
| (setq x 10)
| (setq list '(a b c))

Actually, if I may be a bit pedantic for a moment, SETQ does *NOT*
"bind a symbol to a value". Instead, for a symbol that is already
bound to a lexical or dynamic variable (a place that can hold a value),
it changes the value of that variable, also known as "assigning",
"storing", or "writing" a new value into a variable. (See CLHS "SETQ".)

This is a subtle point, confusing because we ordinarily don't ever
see the "variable" (the place) itself [except in Lisps with locatives].
The CLHS doesn't say it all that well in "Symbols as Forms",
and is IMHO downright misleading sometimes (e.g., in "Variable
Names as Places", or the Glossary entry for "binding"). This is one of
the areas that the Scheme standard explicates more clearly (again, IMHO),
in a way which applies to CL as well. Eliding off-topic bits, from R5RS
3.1 "Variables, syntactic keywords, and regions":

    An identifier may name a ... location where a value can be stored.
    ... An identifier that names a location is called a variable and is
    said to be bound to that location. The set of all visible bindings
    in effect at some point in a program is known as the environment
    in effect at that point. The value stored in the location to which
    a variable is bound is called the variable's value. By abuse of
    terminology, the variable is sometimes said to name the value or
    to be bound to the value. This is not quite accurate, but confusion
    rarely results from this practice.

Except when it does...  ;-}

The term "bind" means to create a *new* association between a name
(symbol) and a place (that can hold a value) -- this new association
is called a "variable". Usually the operation of binding *also* sets
(or assigns or writes) an initial "value" into the place denoted by
the variable. The value can later be *overwritten* by a subsequent
assignment [except for "constant variables"] with SETQ, but such
overwriting does *NOT* change either the place itself or the binding
between the symbol and the place.

[To add to the confusion, in CL "rebinding" a special variable with
LET or LAMBDA, etc., does *NOT* create a new association between
name & place (a new binding). Rather, it saves the old value and
*sets* a new value into the original variable but arranges for the
old value to be restored at the end of the scope of the rebinding.]

To say it one more way, SETQ does not change the variable [or the
binding]; it changes the value that the variable when return when
subsequently read.

So I would rephrase your first two sentence like so:

     SETQ is the simple method of storing a new value into the variable
     represented by a symbol. Keep in mind, that's all it does. It stores
     a new value into a lexical or dynamic variable.

| SETF is an extendable macro. It is a macro that expands to the proper
| form required to bind "something" to a value.

Likewise, this should be changed to remove the word "bind":

    SETF is an extendable macro. It is a macro that expands to the
    proper form required to store a new value into "something",
    conventionally called a "place".

The rest of what you wrote [including examples] is fine.


Rob Warnock			<>
627 26th Avenue			<URL:>
San Mateo, CA 94403		(650)572-2607