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)
  • 5
    But then how will you ever learn to use cadr, cadar, and cddar? – abo-abo Jan 14 '15 at 22:54
  • 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 as and ds gets unwieldy pretty quickly... – phils Jan 14 '15 at 23:29
  • 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 2:39
  • 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 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 2: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 7:50
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.)

|improve this answer|||||
  • 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 0:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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