John Kitchin has another video up on his Scimax Channel. This time, it’s about elementary Elisp data structures and their manipulation. He considers
- Strings
- Lists
- Vectors
- Alists
- Plists
- Hash tables
Strings aren’t really a data structure in the sense of the others but they are, in effect, character arrays and it’s possible to extract or insert data into a string.
The premier Lisp data structure is, of course, the list. After all, its name appears on the marquee. Kitchin describes the need and use of quoting and quasiquoting and various ways of accessing list elements. I’m not really a Lisp old-timer but I’ve been at it long enough that I still prefer the car
and cdr
primitives to the newfangled inventions such as first
, tail
, cl-first
, and all the rest. If I need an element after the third or fourth, I’ll use nth
or elt
. Kitchin likes the cl-*
constructs and as usual Emacs lets you have it your way.
Like me, Kitchin doesn’t use vectors very often and doesn’t have much to say about them but they are simple and easy to use.
Alists and plists are two variations on the same theme. They’re both lists of key/value pairs differing in their representation and access means. A good argument can be made that we don’t need both but they exist for hysterical raisons and Lispers tend to use them both.
Finally, there are hash tables. The idea is pretty well known by now although they are commonly called dictionaries in other languages. They are, again, key/value pairs but have a generally \(O(1)\) lookup time while alists and plists are \(O(n)\). That doesn’t matter for small lists but with hundreds or thousands of items the \(O(n)\) can add up so hash tables make sense for that case.
Even if you’re familiar with using hash tables in Elisp, you may not be familiar with using “reader notation” for initializing a hash table. It really makes sense only for small hash tables—or for writing out and then later reading in a hash table.
The video is just short of 26 minutes so plan accordingly. As usual with Kitchin’s videos, it’s well worth the time to watch it even if you’re an experienced Elisper.