Search code examples
listclojurelispconscdr

How do lisps that prefer first and rest to car and cdr approach combinations like cdaddr?


One of the great schisms in the Lisp community is if we should have car and cdr or first and rest. One of the benefits of the traditional car and cdr is that we can combine them to produce pronoucible functions like cdaddr. How do Lisps that do not use car and cdr, such as Clojure, typically form combinations like this with first and rest? Is there any consensus?


Solution

  • Clojure, at any rate, simply has no need for caddaadr and friends, because nobody builds data structures out of just cons cells. The language does have combinations of any two of first and next, named ffirst, fnext, nnext, and nfirst, which were added very early on I suppose because it was assumed we'd want something like cadr, but I never see them used in real life. Instead destructuring is used quite often.

    On the rare occasions where you need to reach deeply into a structure built of nested sequences, destructuring often still produces readable code but also writing it out longhand is no great burden. It's also a good hint to you that maybe you should abstract thing a bit more rather than working with so many layers of primitive combinators directly.