Hacker News new | past | comments | ask | show | jobs | submit login

The nice thing with CAR and CDR is that you can have combinations like CADR, which is (CAR (CDR A-LIST)). Many structures are rarely linear (say a s-exp repr of a JSON object), and the C*R family of macroes are really useful.



"CADR" should be properly called "second" instead of this dense mnemonic you have to decipher every time you see it.


Lisp usually provides functions like first, second, third, ... CADR would be found in old code or code which works on the cons-level.


Not all C*R functions are linear list access. I guess both those and FIRST, SECOND, etc. have uses. There's no need for ideological fights about them.


What do you suggest for CDAR then? Or CAAR?


> What do you suggest for CDAR then? Or CAAR?

A user-defined function with a name indicating what you're reaching for. Occasionally you might want to write a generic cons handling function where all you can say is that it's the car of the car, but those instances are extremely rare. The only place you should be writing car and cdr compositions is in one line wrapper definitions, and there the benefits (and drawbacks; it doesn't really matter which you use) of the c*r composition functions don't really show themselves.

edit: It's the same logic as using first and rest when you're dealing with a list (that is represented with conses). If you're handling parsed JSON, why would you be talking about cars and cdrs?


And where did I say to use them everywhere? I usually use them in let bindings decomposing complex data structures:

  (let* ((data '((user1
                  (email . ("bob@dom1" "bob@gmail" "hey@bob.com"))
                  (sshkeys . ((default . key1)
                              (alt1 . key2) (alt2 . key3))))))
         (user (cdr (assoc 'user1 data)))
         (email (cadr (assoc 'email user)))
         (sshkey (cdadr (assoc 'sshkeys user))))
    (list email sshkey))
The thing is one would be (rest (first (rest (assoc ..., the other is (cdadr (assoc ..., which are equivalent in effect (and the latter has way less parens). I dont understand why there's an ideological you should use this or you should use that thing going on. C*R stuff is an abstaction over combinations of FIRST/CAR and REST/CDR, and I dont see why they should be avoided at all. They've got their place, and all this thread is full of arguments backed by mere taste.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: