Author Topic: nth or car?  (Read 4873 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: 10648
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: 10648
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: 8723
  • 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.