6 ms·
As a non-Lisper, I can assure you beyond a shadow of a doubt that having "car" & "cdr" show up in the first few sections of a tutorial is not something that win
by jerf 12y ago
As a non-Lisper, I can assure you beyond a shadow of a doubt that having "car" & "cdr" show up in the first few sections of a tutorial is not something that wins people over to Lisp.
I don't mean that I don't understand it. I even know they come from assembler instruction names. And they still look weird to me and still have no meaning despite having read what the abbreviation expands to three or four times.
You'd be far better off shipping "first" & "rest" and whispering to the old hands that you're sure they know how to "fix" the problem in their code, than presenting new people with car & cdr and then try to wedge an explanation into a brain that just threw an exception and went "Wait, what?"
- coliveira 12y agofirst and rest are already part of the language, no change necessary. You just need to use books that teach using first and rest instead of car and cdr.
- pnathan 12y agoHuh. I like the distinction of car/cdr meaning pointers vs first/rest meaning "start of list/rest of list". It weirded me out at first, but I came to really appreciate it.
- TeMPOraL 12y agoIndeed. It lets you clearly express in code that those cons cells are actually part of a different structure than a list.
- jwmc 12y agoYep. It's good to have both - the car is the first of a list, and the cdr is the rest, but a cons isn't always a list. Having first and rest exclusively would be short-changing us conceptually.
- beeworker 12y agoAh, but how would you easily access the number 6 out of the nested list '((((5 4 3 2 1) 6) 7) 8 9) without one of the cute compositions of "car" and "cdr"? In this case, "cadaar". (Rhetorical question. "car" and "cdr" are neat for their cute composition, but I would never inflict that on someone first learning the language.)
- ajuc 12y ago(def x ((((5 4 3 2 1) 6) 7) 8 9)) (get-in x (0 0 0 1)) Probably botched quoting, don't know common lisp, only clojure.
- chongli 12y agoOne word: lenses.
- Flow 12y agoAnd how would that look for this particular example?
- chongli 12y agoDefinitely not as terse but far more general, flexible and powerful. With car and cdr you're restricted to working on cons cells while lenses are generic. Some examples (from Haskell): >>> ((((5,4,3,2,1),6),7),8,9) & view (_1 . _1 . _2) 6 >>> ((((5,4,3,2,1),6),7),8,9) & (_1 . _1 . _2) .~ 4 ((((5,4,3,2,1),4),7),8,9) >>> let foo = ((((5,4,3,2,1),6),7),8,9) >>> foo & view (_1 . _1 . _2) 6 >>> let six = _1 . _1 . _2 >>> foo & view six 6 >>> foo & six .~ 4 ((((5,4,3,2,1),4),7),8,9) >>> foo & six .~ "abc" ((((5,4,3,2,1),"abc"),7),8,9)
- GregBuchholz 12y agoFor the heck of it, I though I'd try it in scheme with pattern matching... (let ((x '((((5 4 3 2 1) 6) 7) 8 9))) (syntax->datum (syntax-case x () ((((_ p) _) _ ...) #'p))))
- agumonkey 12y agoI know and agree, but they have some weird syntactic/linguistic quality: - both 3 letters each - only differ by 1 letter a|d None of the alternatives have these symmetry, and it (very subjectively) shows in the code.
- philh 12y agofst/rst, although having the difference in the middle also seems aesthetically appealing to me.
- gjm11 12y agoNeat, but unfortunately the equivalent of CADR (second item in list: first-of-rest) is then FRST, which I think would just be too easy to misinterpret as meaning "first" when reading or writing code in a hurry.
- agumonkey 12y agoI don't know if cr shortcuts are still used often nowadays (SICP advocates for abstraction layers, accessor functions, CL has destruct-bind, and ml uses pattern matching to dig into data).
- gjm11 12y agoNor do I -- but I'm pretty sure that those shortcuts are pretty much the only good reason for caring much about having names that resemble CAR/CDR in the way FST/RST do. (For any other purpose, I think FIRST/REST are obviously better than FST/RST.)
- agumonkey 12y agoGood catch. While writing a toy lisp I looked for other names with structural qualities, actually not names, more symbols since these weren't words. Things like Oo/oO, xo/ox or _/__.
- dragonwriter 12y ago