Next: Atoms, Previous: Special Functions, Up: Symbols [Index]
The names that we use to denote functions in LISP, e.g. ‘*’, ‘+’, ‘1+’ and ‘setq’, do not constitute a separate class of object. Rather, these are symbols, just like ‘x’, ‘y’, and ‘foo’. As far as LISP is concerned, the only difference between the first group of symbols and the second is that members of the former group are known to LISP as denoting some function, while those of the latter are not.
Other than the fact that they denote functions built into LISP, there is nothing at all special about the symbols ‘+’, ‘1+’, etc. We can assign values to these symbols just as we can to les prominent symbols:
-> (setq 1+ 17) 17 -> 1+ 17 ->
Since ‘1+’ appears in an argument position in the call to setq
, LISP
completely ignores the fact that the symbol ‘1+’ is normally used to
refer to a function. Instead, the symbol ‘1+’ is assigned a value, as
would any other symbol passed to setq
in this manner. Afterwards,
we see that ‘1+’ evaluates to the value it has just been assigned,
just like a “normal” symbol would.
But what about ‘1+’ ’s ability to act as a function? Let us see if it is still reliable:
-> (1+ 3) 4 ->
Assigning ‘1+’ a value has absolutely no effect whatsoever on its role as a function name.
Symbols serve the dual roles of function identifiers and variable
specifiers in LISP. And it is possible to use symbols for both roles
simultaneously. In principle, symbols can have multiple roles in
LISP, because the LISP interpreter can determine the role served by a
particular symbol from its context. If we ask LISP to evaluate a list
that begins with a symbol, LISP interprets taht symbol as naming a
function; if we ask LISP to evaluate just a plain symbol, LISP
interprets that symbol as referring to a variable. LISP is not
confused by expressions like (setq 1+ 17)
: it knows that setq
is
meant to be used to refer to a function because it occurs in the
beginning of the list; it knows that ‘1+’ is intended as a variable
because it occurs elsewhere.
Since the interpretation of a symbol is clear from context, LISP programmers tend to talk in an informal way about the roles played by symbols.
the variable named by the symbol ‘x’
the function named by the symbol ‘setq’
Next: Atoms, Previous: Special Functions, Up: Symbols [Index]