3

With Emacs Lisp it is possible to store a value and a function within the same symbol.

(setq  foo 6)
(defun foo () (message "blah %d" foo)))

Then you can do:
(symbol-function 'foo) which returns the function (lambda nil (message "blah %d" foo)) and
(symbol-value 'foo) which returns the value 6.

Also you could evaluate (foo) which prints "blah 6".

This is cool, but is there any practical relevance?
(Is there a reasonable usage somewhere?)

1 Answer 1

6

Emacs Lisp is a Lisp 2, which means only just what you described: the same symbol can have separate values as (1) a variable and (2) a function. (We call only #1 the "value" of the symbol.) Some other Lisp variants, such as Scheme, are Lisp 1s.

There are advantages and disadvantages for each. What's the advantage of having separate variable and function values?

I would say that it puts more emphasis on the symbol as being something in its own right - something separate from just a mapping between a name and a (variable) value.

A Lisp symbol is an object, of sorts. It has a name (symbol-name) and property list (symbol-plist). For convenience, one (Lisp 1) or two (Lisp 2) particular properties have been pulled out and are treated specially, separately from the plist. They are a value as a variable (symbol-value) and, for Lisp 2s, a value as a function (symbol-function).

There's really nothing more to it. Is it especially practical to have both a variable and a function that have the same name? Certainly you can make use of such a feature. Sometimes a variable of the same name is used to hold some particular ("current" or "cached") value of the function or value associated with the function definition. For example, a minor-mode variable's value tells you whether the mode is on or off; a minor-mode's function toggles it on/off.

But there is nothing especially important about such a convenience (or bother, depending on your point of view) - it is really something minor.

I think this is the point: With a Lisp 2, you tend less to think of a symbol as being just a variable. You tend to think of it as being a bit more. To me, the status of "symbol" is elevated a bit. In a Lisp 1, a symbol is more directly and immediately thought of as a name for a (variable) value.

In a way, maybe this emphasizes the behavioral aspect of a symbol. Lisp 1 is more "functional", if you like. Lisp 2 is arguably more "symbolic". But that's perhaps a personal way to look at the difference.

I think there's really no great answer to the question of practical advantage. Any example that might show some convenience of one or the other can be countered. Certainly that's true for any purported advantage for Lisp 2.

You can almost think of a Lisp 2 as having two separate namespaces. That allows the same name to be mapped to both a variable and a function. I say "almost" because they are linked by the same symbol, which means they can be manipulated together in some ways, and they share the same plist.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.