7

http://www.gnu.org/software/emacs/manual/html_node/elisp/Plists-and-Alists.html gives some supposed differences between plists and alists. Basically it comes down to:

  1. Alists can be used as stacks, where values are updated by adding a cons cell, allowing to restore previous values by deleting the topmost cons cell.
  2. Plists are faster for storing information about symbols because they are typically short compared to storing everything (for all symbols) in a single alist.

The first point makes sense. While the implementation of plist-get allows using plists in such a stack-like manner, by adding new values with

(push VALUE PLIST)
(push KEY PLIST)

removing such a pair would be more involved compared to alists where one can just write

(setq ALIST (delete (assq KEY ALIST) ALIST))

though even that could be changed by defining a function for it.

The second point however seems incorrect too me, as the same advantage would apply for alists, if there was a symbol-alist function instead of symbol-plist. Additionally, in my experience plists are typically a bit slower than alists of the same size.

Hence I was wondering why emacs uses both conventions for storing key-value pairs, when using one would suffice.

Clarification Since it was raised in the comments: It seems largely a historical issue (emacs lisp was able to do both well, so people did both), but I'd still be interested in whether there was some rationale in the development of emacs to include builtin functions for handling both. Of course my view may be strongly colored by the Zen of Python (13th line) here to even raise the question.

5
  • 1
    Some people dislike association lists (hence "improper" lists) because it would've been more uniform, if all lists ended in nil. But I think asking why Emacs has both isn't a good question. It has both because you can create both using cons function. Emacs simply deals with consequences of this feature.
    – wvxvw
    Aug 19, 2015 at 14:29
  • For the record, my comment has nothing to do with karma points.
    – wvxvw
    Aug 19, 2015 at 14:34
  • My guess is that my original formulation of the second reason being "nonsenical" was considered offensive. If that was the reason I can even agree I guess. It would help though, if comments were given.
    – kdb
    Aug 19, 2015 at 15:30
  • 2
    It looks like you're getting votes to close as an opinion-based question. Let me suggest you edit your post in one of two ways. First, if you're interested in the historical decision to support both, clarify that that's your core question. Second, if you're actually interested in when to use plists vs alists, revise the question to something like "under what conditions should I use a plist rather than an alist?" (although even that is pretty general).
    – Dan
    Aug 19, 2015 at 15:37
  • 1
    Hence I was wondering why emacs uses both conventions... That's another way of saying freedom: emacs implements all conventions, you choose which one to follow.
    – Emacs User
    Aug 19, 2015 at 16:05

2 Answers 2

2

Fundamentally the explanation is historical. Lisp 1 stored the definition of a function on the function symbol's plist, and stored the values of all dynamically scoped variables (the only kind) in a big alist. It could have been done in any of the other three ways.

By convention, many people treat alists as persistent (mutation-free) whereas "disembodied" plists (that is, ones not attached to a symbol) are usually mutated.

1

The Emacs Lisp Manual gives a good explanation. Here's a brief summary:

  • If you are associating info with something that is already a Lisp function or variable, plists are faster because they're usually smaller and tied directly to the symbol. But the plist of a symbol is a global namespace, so different packages might conflict.
  • If you are trying to have general purpose information, an alist works better since keys can be anything, you can use it as a stack, and because different packages can have their own alists.

Association lists (*note Association Lists::) are very similar to property lists. In contrast to association lists, the order of the pairs in the property list is not significant, since the property names must be distinct.

Property lists are better than association lists for attaching information to various Lisp function names or variables. If your program keeps all such information in one association list, it will typically need to search that entire list each time it checks for an association for a particular Lisp function name or variable, which could be slow. By contrast, if you keep the same information in the property lists of the function names or variables themselves, each search will scan only the length of one property list, which is usually short. This is why the documentation for a variable is recorded in a property named `variable-documentation'. The byte compiler likewise uses properties to record those functions needing special treatment.

However, association lists have their own advantages. Depending on your application, it may be faster to add an association to the front of an association list than to update a property. All properties for a symbol are stored in the same property list, so there is a possibility of a conflict between different uses of a property name. (For this reason, it is a good idea to choose property names that are probably unique, such as by beginning the property name with the program's usual name-prefix for variables and functions.) An association list may be used like a stack where associations are pushed on the front of the list and later discarded; this is not possible with a property list.

1
  • 2
    I've read this (see link at the beginning of the question) actually already and found it unconvincing. The cited performance difference only applies because the Emacs developers chose to attach a plist to symbols, but if there was a symbol-alist instead it would yield the same advantage. On the other hand plists can be used as stacks just as well, though I agree that their structure doesn't lend itself to the usecase as much (it would require the definition of more complicated functions to facilitate this usecase compared to alists).
    – kdb
    Aug 27, 2015 at 9:31

Your Answer

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

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