5 ms·
CAR and CDR
- csmuk 13y agoStill prefer first and rest. At least that has meaning outside the first use of car and cdr.
- outworlder 13y agoThey are better, until you try to compose them :)
- mercurial 13y agoI'll take head/tail over first/rest, but first/rest is not too bad.
- csmuk 13y agoI thought that for a bit until I realised tail is a bit ambiguous as it's used in the Unix space to mean the "last n" items and in some data structures as the last item pointer.
- mrottenkolber 13y agoIronically in CL LAST does what UNIX tail does.
- csmuk 13y agoYeah which is where SBCL and me got into to a bad argument :)
- mrottenkolber 13y agoYou probably were looking for LAST1 (e.g. (first (last ...))). But why SBCL? LAST should behave the same on all implementations.
- csmuk 13y agoThat's just the variety of CL that I used.
- lgarron 13y agoI'm a fan first/rest because of Mathematica. It has four functions that all end in "st", which means I can always recall the one I need: First, Rest, Most, Last (Results on Range[4], respectively: 1, {2, 3, 4}, {1, 2, 3}, 4.)
- draegtun 13y agoRebol comes with first & last but not the others. >> a: [1 2 3 4] == [1 2 3 4] >> first a == 1 >> last a == 4 This is how you could add rest & most: >> rest: func [s] [next s] >> most: func [s] [copy/part s back tail s] >> rest a == [2 3 4] >> most a == [1 2 3] Rebol unifies all this under what it calls a series - http://www.rebol.com/docs/core23/rebolcore-6.html http://www.rebol.com/docs/core23/rebolcore-6.html
- lcedp 13y agoIn Clojure it's almost the same: first, rest, butlast, last
- mrottenkolber 13y agoIt has nothing to do with preference. FIRST and REST are to be used when working on a LIST, CAR and CDR when working on CONS cells.
- csmuk 13y agoYes but every structure that a cons represents can be expressed in terms of first and rest. That includes tables, singly linked lists, doubly linked lists, binary trees from experience. Car and Cdr are arbitrary and legacy terms.
- mrottenkolber 13y agoArbitrary and legacy, sure, but they do serve their purpose by communicating different expectations when using a single operator. CDR? result can be anything. REST? author probably thinks the result will always be a LIST. I can think of other examples using SETF but I think I made my point clear.
- xmonkee 13y agobut saying "cudder" is so much more fun
- agumonkey 13y agoI can't disagree with that, but after learning clojure I realized that I interned `car` and `cdr` as symbols of which I like : - the symmetry: 'c[ad]r' - thus same size - small size (3) and ended up being annoyed by `first` and `rest`. I see them as APL-like characters now. ps: based on that, head/tail, hd/tl, fst/snd (seen in ml dialects) are also good candidates.
- deleted 13y ago[deleted]
- yati 13y agoSame here, but cons cells can be used to model things other than lists, where semantically first and rest don't make sense.
- thaumasiotes 13y agoI once wrote an in-place quicksort in common lisp where I kept pointers to locations of interest in the list as halves of a cons cell. Was kind of fun coming across it later and figuring out what it did. That cons cell made no semantic sense as a linked list; it was just a pair of pointers.
- RBerenguel 13y agoI always forgot which was which until I actually forced me to use them for a while (and "while" was too long). The only cool thing of the names is that you can get a cryptic sticker "My other car is a cdr" that no-one will understand but will make you jiggle each morning
- petercooper 13y agoMine is "car" and "CarDriveR" (implying first comes the car, then its driver). "CorriDoR" is another one which implies the extra length a bit better.
- RBerenguel 13y agoI just hard-wired into my brain they are car and cdr (I read it as "cadar," with both a's quite like the a in car, Spanish a for me.) In a cons list, you have the car (first) and then the cadar. Think it enough times and it sinks eventually.
- philwelch 13y agoBut "cadar" is also a thing: it's the car of the cdr of the car.
- RBerenguel 13y agoI know, I'm no lisp beginner, but the pronunciation of cdr is almost like "cadar". I just "Spanishised" it.
- MichaelGG 13y agoWhat's wrong with an ordinal sort? car sorts before cdr.
- petercooper 13y agoGood point!
- kang 13y agoquote used to mark data as opposed to code can be tricky while using CDR & CAR : http://stackoverflow.com/questions/18697105/cdadr-on-nested-data-list-in-lisp http://stackoverflow.com/questions/18697105/cdadr-on-nested-...
- derleth 13y agoI like them because they don't guide thought: They're abstract names for abstract concepts. They don't impose any interpretation on the ideas, they simply present them for what they are. Having to break down a less-useful conceptual model is even more difficult than building a more-useful one.
- joeygibson 13y agoI first learned Lisp so long ago that car and cdr are still ingrained in my head. first and last feel somehow wrong, to me.
- elrzn 13y agoIt's a pity they're not standard in many other languages. I'm not a fan of having to type first/rest or playing around with syntax soup and I really miss caddadadaddaaaaaring around. sub car { $_[0] } sub cdr { shift; @_ } # every utils module ever
- csmuk 13y agoNoooooooooooooooooooooooooooooooooooooooo. Perl can be written ugly enough without introducing car and cdr to it!
- draegtun 13y agoPlonk that into an autobox module (like perl5i and/or autobox::Core) and enjoying even more caddadadaddaaaaaring around! For eg: use perl5i::2; sub autobox::Core::ARRAY::car { $_[0]->[0] } sub autobox::Core::ARRAY::cdr { my $last = $#{$_[0]}; wantarray ? @{$_[0]}[1 .. $last] : [@{$_[0]}[1 .. $last]]; } then... my @a = 1..4; @a->car; # 1 @a->cdr->cdr->car; # 3 @a->cdr; # 2, 3, 4 my @x = @a->cdr; @x->cdr; # 3, 4 Some refs: https://metacpan.org/pod/perl5i https://metacpan.org/pod/perl5i | https://metacpan.org/pod/autobox::Core https://metacpan.org/pod/autobox::Core
- Syssiphus 13y ago:* <3 u LISP