Author Topic: Discussion: Concatenations of car and cdr  (Read 5837 times)

0 Members and 1 Guest are viewing this topic.

nivuahc

  • Guest
Discussion: Concatenations of car and cdr
« on: April 22, 2005, 11:38:42 AM »
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

Quote from: Autodesk
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:

Quote from: Autodesk
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:

Code: [Select]
(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? :P

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 results (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?

whdjr

  • Guest
Discussion: Concatenations of car and cdr
« Reply #1 on: April 22, 2005, 11:44:12 AM »
hmmmmmm???!!!! :?

nivuahc

  • Guest
Discussion: Concatenations of car and cdr
« Reply #2 on: April 22, 2005, 11:50:38 AM »
Maybe I should have prefaced that with this, for those who have no clue what car or cdr are all about... I shouldn't make assumptions like I do. :oops:

car - Returns the first element of a list
cadr - Returns the second element of a list
caddr - Returns the third element of a list

cdr - Returns a list containing all but the first element of the specified list

daron

  • Guest
Discussion: Concatenations of car and cdr
« Reply #3 on: April 22, 2005, 11:53:36 AM »
Like you, I don't have to use them THAT much. When I do, I test. As far as keeping them straight, I just remember the definitions for car, cadr and cdr. The rest just fills itself in as I use them.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Discussion: Concatenations of car and cdr
« Reply #4 on: April 22, 2005, 11:54:20 AM »
To me it's "every instance of 'd' will return a remainder of the list, whereas every instance of 'a' will return the first item from the list".

I resolve what the composite construct will return by reading it from right to left.

e.g.

(caddr lst) = (car (cdr (cdr lst)))
(cadadr lst) = (car (cdr (car (cdr lst))))
(cdadr lst) = (cdr (car (cdr lst)))

I just read this and it's of no use to anyone, but it's how I do think about it.

:roll:
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Discussion: Concatenations of car and cdr
« Reply #5 on: April 22, 2005, 11:55:49 AM »
Quote
Do you have a way to remember all of that junk without turning to the book each time?

Nope ... :roll:

*click* acad_dev.chm
TheSwamp.org  (serving the CAD community since 2003)

whdjr

  • Guest
Discussion: Concatenations of car and cdr
« Reply #6 on: April 22, 2005, 11:59:16 AM »
Yeah...I know what they are and how to use them.  You just reminded me of my wife...she's always doing that word association thing.  That doesn't work for me, I can't remember the words I associated with the letters.  For me I just read it backwards.

nivuahc

  • Guest
Discussion: Concatenations of car and cdr
« Reply #7 on: April 22, 2005, 12:00:04 PM »
Quote from: MP
To me it's "every instance of 'd' will return a remainder of the list, whereas every instance of 'a' will return the first item from the list".

I resolve what the composite construct will return by reading it from right to left.

e.g.

(caddr lst) = (car (cdr (cdr lst)))
(cadadr lst) = (car (cdr (car (cdr lst))))
(cdadr lst) = (cdr (car (cdr lst)))

I just read this and it's of no use to anyone, but it's how I do think about it.

:roll:


No, it's useful. It shows that you're probably way too smart to have me as a friend. What're you doin? Slummin'? :P

I am constantly fascinated with the way your mind works Michael.

nivuahc

  • Guest
Discussion: Concatenations of car and cdr
« Reply #8 on: April 22, 2005, 12:02:12 PM »
Quote from: Mark Thomas
Quote
Do you have a way to remember all of that junk without turning to the book each time?

Nope ... :roll:

*click* acad_dev.chm


Me too!

As smart as you are, that makes me feel a whole lot better! :D

whdjr

  • Guest
Discussion: Concatenations of car and cdr
« Reply #9 on: April 22, 2005, 12:03:21 PM »
Quote from: Mark Thomas
Quote
Do you have a way to remember all of that junk without turning to the book each time?

Nope ... :roll:

*click* acad_dev.chm


I have it as a shorcut on my desktop.

nivuahc

  • Guest
Discussion: Concatenations of car and cdr
« Reply #10 on: April 22, 2005, 12:05:28 PM »
Quote from: whdjr
Yeah...I know what they are and how to use them.  You just reminded me of my wife...she's always doing that word association thing.  That doesn't work for me, I can't remember the words I associated with the letters.  For me I just read it backwards.


I figured you did Will, but I read your post before I saw that it was you who posted it... and I immediately thought that I had really confused someone who had no idea what I might be talking about. For the benefit of those who've yet to cut their teeth on Lisp, I felt I ought to throw that in there. It wasn't directed towards you, and I hope you didn't take it that way.  :oops:

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Discussion: Concatenations of car and cdr
« Reply #11 on: April 22, 2005, 12:05:44 PM »
You couldn't be more wrong Chuck, I wish my synapses (all three of 'em) fired half as fast as yours.

:lol:
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

nivuahc

  • Guest
Discussion: Concatenations of car and cdr
« Reply #12 on: April 22, 2005, 12:08:53 PM »
Then what does it say about me that I had to look up the definition of synapses?

Hmmmmmmmmm?  :?

TimSpangler

  • Water Moccasin
  • Posts: 2010
  • CAD Naked!!
Discussion: Concatenations of car and cdr
« Reply #13 on: April 22, 2005, 12:47:52 PM »
doesn't (nth Pos List) give you that same only with an expected resualt

(setq r '(a b c d e f g))

(nth 4 r)
d

Is this way no good?  This is usually how I get there from here.
ACA 2015 - Windows 7 Pro
All Comments and Content by TimSpangler, Copyright © 2016

daron

  • Guest
Discussion: Concatenations of car and cdr
« Reply #14 on: April 22, 2005, 12:51:36 PM »
I'd say it's similar. What about nested lists? Wouldn't mapcar <(MultipleAPlicationCAR) also work in a similar way. Of course, foreach would also do the same trick in a different manner. How many ways are there to skin this cat?

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Discussion: Concatenations of car and cdr
« Reply #15 on: April 22, 2005, 04:45:22 PM »
Quote from: TimSpangler
doesn't (nth Pos List) give you that same only with an expected resualt

(setq r '(a b c d e f g))

(nth 4 r)
d

Is this way no good?  This is usually how I get there from here.


What?
You had better check your results. :shock:
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

daron

  • Guest
Discussion: Concatenations of car and cdr
« Reply #16 on: April 22, 2005, 04:48:21 PM »
Hahahaha. I didn't see that. Pssst! Tim, 'e' would be the expected result. ROFL.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Discussion: Concatenations of car and cdr
« Reply #17 on: April 22, 2005, 04:50:03 PM »
Maybe it's a customized nth function. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

daron

  • Guest
Discussion: Concatenations of car and cdr
« Reply #18 on: April 22, 2005, 04:50:52 PM »
Party pooper.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Discussion: Concatenations of car and cdr
« Reply #19 on: April 22, 2005, 04:55:11 PM »
Tim,
nth is what i use most of the time.
When it gets beyond car cdr I look in the book, big green tab hangin out.  :)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.