next up previous
Next: Variable Operations Up: Variables Previous: Variables

Symbols

In other languages one has ``variables''. In Lisp we have ``symbols''. Symbols are named locations. What can go into that location is quite different and intriguing. One can visualise a symbol as shown in Table 3


 
Table 3: The parts of a Lisp Symbol.
name value as a definition as space for
  variable a function property lists
 

A symbol object in Lisp is the whole thing with all four ``cells''. The most striking feature of a symbol is that there are distinct cells to hold its value as a variable and its value as function definition. What this means is that any name can have both kind of bindings. In other words, foo can have the value 10 associated with it, and can also have some function definition. There are predefined functions in Elisp to access the different cells of a symbol. (setq foo 10) will assign the value 10 to foo's variable cell. (fset 'foo 'bar) will assign the function definition of bar to foo. Note that these are not the only ways to accomplish this effect.

You may ask, reasonably, ``if the same name can act as a variable and a function, will it not create problems at run-time?'' It turns out not to be a problem. The lisp interpreter is able to tell unambiguously when a symbol should be accessed as a variable and when it should be accessed as a function. The simple rule is this: if a (non-quoted) symbol is the first expression inside a bracketed expression, then it should be accessed as a function, and in all other cases, it should be accessed as a variable. So, assuming foo has both variable and function bindings, the first example accesses foo as a function and the second example as a variable

(foo 10)
(bar (+ foo 10))

A symbol has a fourth cell: some space to hold property lists. This is a convenient place to hold name/value pair property data relevant to the symbol. For e.g. many Elisp commands come disabled by default. when a disabled Elisp command is invoked, Emacs prompts the user and asks him if he really intended to invoke this command and whether the command should be enabled for all future sessions etc. The information about the disabled/enabled state is maintained in the fourth cell of the symbol. This is just one example to give you an idea about the usefulness of this property list business. For the curious folks, put and get are the Elisp functions that can be used to access the property-list area of a symbol. We shall not go into them in any more detail.

To sum it up, a Lisp symbol can have both a variable binding and a function binding. When one just talks about 'variables', one refers to the variable binding of the symbol. With that understanding, let's look at the most important thing one needs to know about what Elisp lets us to with them.


next up previous
Next: Variable Operations Up: Variables Previous: Variables
Sriram Karra
2005-01-06