How to Alias Car and Cdr and Why You Probably Shouldn’t

A couple of days ago, I wrote about the minor controversy concerning the names of the Lisp functions car and cdr. Some people, mostly non-Lispers but also some who use Lisp, dislike the names and think they should be changed to something more mnemonic like head and tail or first and last. Paul Graham disagrees and tweeted an excellent, pithy argument as to why. I agreed with Graham and the post was really about his tweet.

Of course, being Lisp, it’s easy to alias the names to whatever you like. However, there is, at least for Emacs, a practical reason that you shouldn’t do that or at least that you should know what you’re doing before you try. Chris Wellons, who has a profound understanding of Emacs and Emacs Lisp has a very enlightening post as to why adding something like

(defalias 'head #'car)

to your init.el file is a bad idea. The reason for that is that Emacs Lisp recognizes the importance of car and cdr and has special byte code operators for them. When you alias car to something like head, the optimizer can no longer be sure that the meaning won’t change and therefore turns the head reference into a function call. Wellons has the details but the TL;DR is that the result runs twice as slowly.

This is Emacs so it is, of course, possible to do the alias and still have the result use the car byte code but you have to tell the optimizer explicitly to do so. Again, see Wellons’ post for the details.

Wellons’ post is a slight expansion of his comment to the reddit post asking how to alias car that I referenced in my original post. His explanation is generally useful, though, so I’m glad he made it a separate post.

This entry was posted in General and tagged . Bookmark the permalink.