Author Topic: nth or car?  (Read 4875 times)

0 Members and 1 Guest are viewing this topic.

curmudgeon

  • Newt
  • Posts: 194
nth or car?
« on: May 07, 2009, 02:04:22 PM »
silly question.

I have a list, say
Code: [Select]
(setq the_list '(1.0 2.2 5.77 5.0 9))the length of the list is unknown, until after it is created.

I have been rotating the list.
Code: [Select]
(setq the_list (append (cdr the_list)(list (list (car the_list)))))
so the above, after my improvised rotate command would look like:
(2.2 5.77 5.0 9 1.0)
unless memory failed me, and I have typos in that last snippet, it works.

now for the silly question.
in such a list, is there any PRACTICAL difference between (car the_list) and (nth 0 the_list)?

 :ugly:

FYI
the lists are the vertexes I strip from a LWPOLYLINE, so they are actually pairs, like:
((3126.9 2991.44) (3192.75 3142.98) (2988.14 3177.04) (2969.32 3049.0) (2969.32 3026.68))

that just might matter, but I don't (yet) see how in might matter.
« Last Edit: May 07, 2009, 02:07:35 PM by curmudgeon »
Never express yourself more clearly than you are able to think.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: nth or car?
« Reply #1 on: May 07, 2009, 02:23:55 PM »
I would use:

Code: [Select]
(setq lst2 (list (cdr lst)(car lst)))
I think nth is slower than car.
Also append is slower than lst.

IMO 8-)
« Last Edit: May 07, 2009, 02:28:08 PM by CAB »
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.

JohnK

  • Administrator
  • Seagull
  • Posts: 10649
Re: nth or car?
« Reply #2 on: May 07, 2009, 02:25:35 PM »
readability? eh, not really.
speed? meh, car is faster but we would be splitting hairs at this point.

No, not really. Mostly personal preference i would say. I prefer to use CAR but i use nth too.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Re: nth or car?
« Reply #3 on: May 07, 2009, 02:43:19 PM »
I use nth once I get to the point that the car/cdr/cadr's confuse me....which is right around the car/cdr stage :-)

JohnK

  • Administrator
  • Seagull
  • Posts: 10649
Re: nth or car?
« Reply #4 on: May 07, 2009, 03:36:41 PM »
*lol*
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: nth or car?
« Reply #5 on: May 08, 2009, 12:40:32 AM »
The note I use, cleaned up a bit. :)
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.

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: nth or car?
« Reply #6 on: May 08, 2009, 07:15:39 AM »
This may be of some use to you:

http://ronleigh.info/autolisp/afude09.htm#car

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: nth or car?
« Reply #7 on: May 08, 2009, 08:35:32 AM »
Thanks Lee
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.

Siem

  • Guest
Re: nth or car?
« Reply #8 on: May 08, 2009, 08:45:26 AM »
I agree with CAB on the rotation example, but why rotate the list? It's better programming practice to use nth and change the indexcounter.

If you need just the first element of a list, use car.
If you need the whole list except the first element, use cdr.
If you need any element, use nth.

Forget these unreadable caaddaaddadr constructions.

Best regards,
Siem

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8728
  • AKA Daniel
Re: nth or car?
« Reply #9 on: May 08, 2009, 08:50:30 AM »
Welcome to TheSwamp Siem  8-)

curmudgeon

  • Newt
  • Posts: 194
Re: nth or car?
« Reply #10 on: May 08, 2009, 05:08:39 PM »
thanks to all.

I rotate the list because of the nature of the list, it is a list of vertexes, and so sometimes I want ((car pt_lst)(cadr pt_lst))
and sometimes I may need ((last pt_lst)(car pt_lst)).

 :-P

Never express yourself more clearly than you are able to think.

curmudgeon

  • Newt
  • Posts: 194
Re: nth or car?
« Reply #11 on: May 08, 2009, 05:29:12 PM »

Quote
I think nth is slower than car.
Also append is slower than lst.

CAB?

what is "lst"?
I don't see it in my Vlisp help file.
Never express yourself more clearly than you are able to think.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: nth or car?
« Reply #12 on: May 08, 2009, 05:51:12 PM »
CAB?

what is "lst"?
I don't see it in my Vlisp help file.

I believe he meant cons*, but I could be wrong.

* cons is slower than append
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

curmudgeon

  • Newt
  • Posts: 194
Re: nth or car?
« Reply #13 on: May 08, 2009, 06:15:07 PM »
thanks.

I have ignored the total understanding of cons because it is more complex - one name for a function that behaves differently depending on the type of input is what I mean. I shall take this as an opportunity to improve myself.......
:realmad:

( not mad, just enjoy this little guy's animation. )
Never express yourself more clearly than you are able to think.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: nth or car?
« Reply #14 on: May 08, 2009, 06:16:23 PM »
Sorry for my high speed typing. I often skip letters  :-o
Should be LIST and not lst.
Quote
Also append is slower than list.

as in
Quote
(setq lst2 (list (cdr lst)(car lst)))
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.

curmudgeon

  • Newt
  • Posts: 194
Re: nth or car?
« Reply #15 on: May 08, 2009, 06:21:24 PM »
gotcha, thanks.
 :lol:
this one should be "Wahoo!" instead of "cheesy", I am thinking...
Never express yourself more clearly than you are able to think.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: nth or car?
« Reply #16 on: May 08, 2009, 06:26:49 PM »
?? weird that you would compare the performance of list and append ??
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: nth or car?
« Reply #17 on: May 08, 2009, 10:14:46 PM »
 8-)
curmudgeon used append to get his result & I used list. Made sense in my little world. :-)
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.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: nth or car?
« Reply #18 on: May 09, 2009, 02:56:32 AM »
Hi all,

list and cons functions are faster than append (no doubt about it) but sometimes append seems to be the best way.

If I don't misunderstand, the goal is to rotate the first item of a list to the end of this list:

(setq lst '(0 1 2 3 4 5 6 7 8 9))
(0 1 2 3 4 5 6 7 8 9) -> (1 2 3 4 5 6 7 8 9 0)

Neither curmudgeon nor CAB given expression do the trick:

(append (cdr lst) (list (list (car lst)))) returns (1 2 3 4 5 6 7 8 9 (0))

(list (cdr lst) (car lst)) returns ((1 2 3 4 5 6 7 8 9) 0)

Right expressions could be:

(append (cdr lst) (list (car lst)))

(reverse (cons (car lst) (reverse (cdr lst))))

which both return (1 2 3 4 5 6 7 8 9 0) and the append expression is faster than the reverse cons reverse one.
Speaking English as a French Frog

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: nth or car?
« Reply #19 on: May 09, 2009, 09:06:21 AM »
gile did the "home work" :-)
Thanks
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.

cyberiq

  • Guest
Re: nth or car?
« Reply #20 on: May 12, 2009, 11:31:50 AM »
Hi,

You can find a little extra comments on this issue at the following adress:

http://discussion.autodesk.com/forums/thread.jspa?messageID=6141668&tstart=0

Regards

curmudgeon

  • Newt
  • Posts: 194
Re: nth or car?
« Reply #21 on: May 15, 2009, 03:52:36 PM »
thanks again guys. gile, I have no excuse, what you reported as "right" with the append version is exactly the syntax I had intended to be typing. sorry. horse, I read the thread you supplied entire. the speed tests were also very interesting.
 8-)
Never express yourself more clearly than you are able to think.