3 ms·
So when you call them first and rest, how do you destructure lists of lists? Actually, now that I think about it, "fire" is much cooler sounding than "cadr".
by jrockway 16y ago
So when you call them first and rest, how do you destructure lists of lists?
Actually, now that I think about it, "fire" is much cooler sounding than "cadr". But not as cool sounding as cadadr.
- pavelludiq 16y agoMaybe im to inexperienced with classic lisps, or maybe im just spoiled by clojure, but i have no idea what cadadr means, is it the rest of the first, of the rest of the first, or the other way around?
- bitsai 16y agoFor things like cadadr, I just chop off the "c" and "r", and use the middle part to figure out the sequence of car and cdr calls. (cADADr x) = (cAr (cDr (cAr (cDr x)))), which would be (first (rest (first (rest x)))) in Clojure.
- chousuke 16y agoI suppose CL/Scheme programmers just learn by heart what those combinations do (ie. what kind of structure they work with), because to me both cadadr and the (first (rest ...)) sequence are equally meaningless at first glance. It's not easy to see what the data structure looks like. In Clojure it's usually more idiomatic to use destructuring, which I think is much more understandable since the form follows the structure that is being accessed. Or you can use get-in, which takes a sequence of keys to dig into a deep associative data structure. It doesn't work with sequences, but building highly nested seqs is rather uncommon in Clojure.
- gtani 16y agoor the "->" ("threading") macro, a recursive solution, or a lib like http://groups.google.com/group/clojure/browse_frm/thread/11a7203021382ad8# http://groups.google.com/group/clojure/browse_frm/thread/11a...
- whyenot 16y agodestructuring-bind ;)
- ataggart 16y agoFor me, the following is a lot more readable than cadadr: (def coll [[1 2 3] [4 5 6] [7 8 9]]) (second (second coll)) ; => 5 And often when I'm digging deep inside a nested data structure, I don't need just one piece of data, so destructuring comes in handy: (let [[[top-left] [_ middle] [_ _ bottom-right]] coll] [top-left middle bottom-right]) ; => [1 5 9]
- silentbicycle 16y agoOr e.g. cadaadr -> frffrst, "first of rest of first of first of rest of" . I don't know if any Lisp uses composed first/rest that way, but if I were implementing a new Lisp from scratch I would probably use that rather than car / cdr. (I prefer pattern matching / destructuring-bind, though.)