2

I'm often obliged to read the documentation before using cdr and car functions. Is there pitfalls I should be aware of before aliasing those functions in my init file? Like:

(defalias 'head 'car)
(defalias 'tail 'cdr)
Drew
  • 75,699
  • 9
  • 109
  • 225
Nsukami _
  • 6,341
  • 2
  • 22
  • 35
  • 5
    But then how will you ever learn to use `cadr`, `cadar`, and `cddar`? – abo-abo Jan 14 '15 at 22:54
  • @abo-abo You're right, those name are just not user friendly nor easy to associate with what the function is doing :\ – Nsukami _ Jan 14 '15 at 23:02
  • 1
    FYI, along with `first` you also get `second`, `third`, ..., `tenth`, which covers some of those functions. I have no problem with `car` and `cdr`, but counting `a`s and `d`s gets unwieldy pretty quickly... – phils Jan 14 '15 at 23:29
  • @phils definitely – Nsukami _ Jan 15 '15 at 00:17
  • 1
    On the contrary (and this might have been @abo-abo's point; dunno): `cadr` etc. are **very** mnemonic and give you an instant picture of just what each of them does. (On the other hand, access macros named specifically for what they mean for your application are often a better idea.) – Drew Jan 15 '15 at 02:39
  • 1
    If you are just accessing elements of a list in a flat, linear manner (i.e., not digging into tree structure), then function `nth` is a good substitute. It is the generic version of `first`, `second`, etc. – Drew Jan 15 '15 at 02:41
  • @Drew mnemonic? I'm probably blind :( or still being struggling to do the difference between car & cdr, I wont be able to see the mnemonic?! – Nsukami _ Jan 15 '15 at 02:47
  • 3
    Maybe you are; dunno. What can I say? ;-) I see an access path through a tree - a very straightforward & simple picture. Maybe something like [this](https://books.google.com/books?id=CIYUAwAAQBAJ&pg=PA49&lpg=PA49&dq=cadr+cdar+cadar+lisp&source=bl&ots=0HG6a1X7E1&sig=N2pGHcCCg0B0vP9Ow4J0RVqY4vM&hl=en&sa=X&ei=HCu3VN3fJsugyASvo4LIDw&ved=0CDUQ6AEwBDgK#v=onepage&q=cadr%20cdar%20cadar%20lisp&f=false) will help. Maybe not. If you don't define meaningful access macros (which is best), do you really prefer `(tail (tail (head (head (tail (head xs))))))` to `(cddaadar xs)`? – Drew Jan 15 '15 at 02:58
  • 3
    I was trying to say that `cadr`, `cadar` and `cddar` are extremely intuitive and mnemonic. `c` and `r` are just filler, `a` means "go left", `d` means "go right". Easy as pie – abo-abo Jan 15 '15 at 07:50

1 Answers1

13

I'd suggest that you (require 'cl) and then use first and rest instead. They already exist, and are the standard alternative names.

They also get expanded to the actual car and cdr byte-code at compilation, whereas your custom aliases would not. (Not that you can't make them -- see the definition of cl--defalias -- but I think sticking to the standard aliases is a better idea.)

phils
  • 48,657
  • 3
  • 76
  • 115
  • For what it's worth, the cleaned up `cl-lib` has `cl-first` and `cl-rest`. – Dan Jan 14 '15 at 23:06
  • Yeah; I just couldn't bring myself to suggest those ones as being *better* alternative names. – phils Jan 14 '15 at 23:32
  • Fair enough -- thought I'd bring it up since `cl-lib` is (I think?) the successor to `cl`. – Dan Jan 15 '15 at 00:31