MP posted some code yesterday and it got me thinking... I know,
dangerous.
Anyway, he used
cdadr in his code and I had to go look it up to remember what, exactly, I should expect as a result. That's because I can't
ever keep those cockeyed functions straight without refering to the manual. The manual, on the other hand, wasn't all that helpful. If I were new to Lisp and I came across this
AutoLISP supports concatenations of car and cdr up to four levels deep. The following are valid functions:
caaaar cadaar cdaaar cddaar
caaadr cadadr cdaadr cddadr
caaar cadar cdaar cddar
caadar caddar cdadar cdddar
caaddr cadddr cdaddr cddddr
caadr caddr cdadr cdddr
caar cadr cdar cddr
I'd be scratching my head in complete confusion. Autodesk goes on to explain themselves this way:
These concatenations are the equivalent of nested calls to car and cdr. Each a represents a call to car, and each d represents a call to cdr. For example:
(caar x) is equivalent to (car (car x))
(cdar x) is equivalent to (cdr (car x))
(cadar x) is equivalent to (car (cdr (car x)))
(cadr x) is equivalent to (car (cdr x))
(cddr x) is equivalent to (cdr (cdr x))
(caddr x) is equivalent to (car (cdr (cdr x)))
Yeah.
That
really clears things up, huh?
So what I was wondering is this... how do you, personally, keep that straight in
your mind? How do you help yourself remember what is what when it comes to the functions above, without looking it up each and every time?
Me? I
rarely, if ever, use those functions. I do it the
hard way, I guess, by stepping through lists using
car,
cadr,
caddr,
cdr and so on. I remember which is which by visualising the list and thinking like this:
car (a b c)
c (
"see")
a result (
a b c)
cadr (a b c)
c (
"see")
a different
result (a
b c)
caddr (a b c)
c (
"see")
a different,
different
result (a b
c)
cdr (a b c)
c (
"see")
different
result
s (a
b c)
And so on...
Yeah, I know, that probably seems silly... but it's how I thought about it back when I first started with Lisp and I guess it stuck.
How about you? Do you have a way to remember all of that junk without turning to the book each time?