Previous Table of Contents Next


2.7. A Chest of Drawers

As we said earlier, the printed representation of Emacs Lisp that you and I read is not the same as the representation seen by the computer.

Indeed, a symbol, such as multiply-by-seven, which we defined previously, or buffer-name, which is a standard part of Emacs Lisp, has four components.

The components can best be understood through a somewhat fanciful representation.

Imagine a chest of four drawers:

  The first drawer has in it a piece of paper with the name of the symbol written on it; this is its printed representation.
  The second drawer has in it the definition of the symbol as a function, which is what is created with defun.
  The third drawer has in it the value of the symbol as a variable, which is what setq sets.
  The fourth drawer has in it a property list, a component we have not yet described. (See section 2.15.)

A single symbol can have any or all of these components. Almost always, it has its name (otherwise you cannot read it), and either a function definition or a value or both, and often a property list.

Here is an example that sets the value of the symbol bouquet to a list when you evaluate the expression:

   (setq bouquet ‘(rose violet buttercup))

In this case, the name of the symbol, called in this context a variable, is bouquet; its value is the list (rose violet buttercup).

Here is a representation of how it is held inside Emacs:

You can determine the value of a variable by evaluating it on its own:

   bouquet     → (rose violet buttercup)

You can also find the contents of the value cell of a symbol—that is, determine its value—by evaluating a list in which the function is symbol-value and the first argument is bouquet:

   (symbol-value ‘bouquet)
     → (rose violet buttercup)

Now let’s return to the function from a previous example, multiply-by-seven. To review, its function definition is

   (defun multiply-by-seven (number)
    “Multiply NUMBER by seven.”
    (interactive “p”)
    (message “The result is %d” (* 7 number)))

Also, to illustrate that the same symbol can hold a value, we will set multiply-by-seven to the value 49 (we could just as well set it to a string such as “Goodbye, world”):

   (setq multiply-by-seven 49)

In a chest of drawers diagram, multiply-by-seven looks like this:

The symbol’s name is multiply-by-seven, as expected.

The symbol’s definition is very nearly like the definition we made using defun, except the list starts with lambda instead of defun and is not properly formatted. The list is not properly formatted to human eyes because the computer does not care; atoms are against parentheses or separated from others by whitespace. (Indeed, in the original, the expression is all on one line, but that would not fit in the diagram, so I fit it onto five lines.)

The list starts with the symbol lambda. This indicates that the list represents a function. You may think of the Lisp interpreter as a detective. He finds a chest of drawers hidden in the attic. In the first drawer is a piece of paper with a name on it, multiply-by-seven. “Ah ha!” he says, “We have the name of a symbol!” Then he finds in the second drawer a list that starts with lambda: “We have a function!” Because that function is in the drawer with the name multiply-by-seven, when you evaluate a list that starts with multiply-by-seven, its associated function definition is carried out.

A second function, such as re-search-forward is associated with a second set of drawers, and has its printed representation in the first drawer and a list, also starting with lambda, in the second. The lambda expression for re-search-forward is the definition that tells the computer what to do.

You can write a lambda expression on its own, as a list, like the one in the diagram. And you can evaluate it. In this context, the lambda expression is an anonymous function; that is, it is a function that has no name. It becomes a function with a name when the lambda expression is placed in the appropriate drawer of a chest that also has a name in its first drawer.

Returning to that chest of drawers in the diagram—the third drawer has a piece of paper in it with 49 written on it. This is the value associated with the name multiply-by-seven.

The fourth drawer in our example is empty; it could contain a property list, which is described in section 2.15.

Each symbol has four components. The four components can be discovered using the four functions symbol-name, symbol-function, symbol-value, and symbol-plist.

Here is an example:

   (symbol-name ‘multiply-by-seven)
      → “multiply-by-seven”

   (symbol-function ‘multiply-by-seven)
      → (lambda (number)
                 “Multiply NUMBER by seven.”
                 (interactive “p”)
                 (message “The result is %d”
                 (* 7 number)))

   (symbol-value ‘multiply-by-seven)
      → 49

   (symbol-plist ‘multiply-by-seven)
      → nil

nil, you will remember, is another representation of (), the empty list. It means there is nothing in the property list in this example.


Previous Table of Contents Next