I think I understand the concept of symbols, as far as how they are related
to functions and global values.
My naive idea is that there must be a pointer in the symbol structure for a
variable just like it has a pointer for a function object and the symbol is
said to be bound (to the variable) in case this pointer points to a valid
object. This is clearly not the case. As David S. Touretzky writes in his
book:
"Symbols are never bound; only variables can be bound."
Now I'm pretty much confused. What is the relation of a variable to the
symbol naming it? How is this relation represented in the symbol's
structure?
What does it exactly mean for a variable to be bound? Is it correct to say
"a variable is bound to the symbol naming it" and if it is not bound to a
symbol then what is it bound to?
Help is kindly required as I would really like to understand this issue.
Thanks,
K�roly
From: Brian Seitz
Subject: Re: Newbie confused about symbols and variables
Date:
Message-ID: <b61p17$4v5$1@tomm.stsci.edu>
Ladv�nszky K�roly wrote:
> I think I understand the concept of symbols, as far as how they are related
> to functions and global values.
> My naive idea is that there must be a pointer in the symbol structure for a
> variable just like it has a pointer for a function object and the symbol is
> said to be bound (to the variable) in case this pointer points to a valid
> object. This is clearly not the case. As David S. Touretzky writes in his
> book:
> "Symbols are never bound; only variables can be bound."
>
> Now I'm pretty much confused. What is the relation of a variable to the
> symbol naming it? How is this relation represented in the symbol's
> structure?
> What does it exactly mean for a variable to be bound? Is it correct to say
> "a variable is bound to the symbol naming it" and if it is not bound to a
> symbol then what is it bound to?
Consider that the same symbol can name a variable and a function (and
more!) simultaneously without any conflict.
CL-USER(21): (defun foo (x) (+ 1 x))
FOO
CL-USER(22): (let ((foo 1))
(values foo
'foo
(funcall 'foo foo)))
1 [value of variable named by the symbol 'foo]
FOO [the symbol 'foo]
2 [the result of calling the function named by the symbol 'foo
with the variable named by the symbol 'foo as an argument]
The symbol 'foo is not bound. The variable *named by* the symbol 'foo
is bound to the value 1. The function *named by* the symbol 'foo is
bound to a function pointer.
When Lisp encounters a symbol, it uses the context to determine what the
symbol is trying to represent. It then uses the symbol name as a key to
lookup the actual variable/function object. The symbol itself has no
"structure" really.
What does it mean for a variable to be bound? It is *not* to correct to
say that a variable is bound to the symbol naming it. The variable is
bound to its *value*, which is possibly nil. If the variable has no
binding in the current context, then it is unbound (which is distinct
from being nil.)
Look up boundp, fboundp, and slot-boundp. One tricky thing is that
boundp only refers to global bindings (not lexical bindings like let
defines), so in the example above foo would not satisfiy boundp, but it
is still lexically bound.