TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: curmudgeon on May 07, 2009, 02:04:22 PM
-
silly question.
I have a list, say
(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.
(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.
-
I would use:
(setq lst2 (list (cdr lst)(car lst)))
I think nth is slower than car.
Also append is slower than lst.
IMO 8-)
-
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.
-
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 :-)
-
*lol*
-
The note I use, cleaned up a bit. :)
(http://www.theswamp.org/screens/index.php?dir=cab/&file=cadr.png)
-
This may be of some use to you:
http://ronleigh.info/autolisp/afude09.htm#car (http://ronleigh.info/autolisp/afude09.htm#car)
-
Thanks Lee
-
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
-
Welcome to TheSwamp Siem 8-)
-
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
-
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.
-
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
-
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. )
-
Sorry for my high speed typing. I often skip letters :-o
Should be LIST and not lst.
Also append is slower than list.
as in
(setq lst2 (list (cdr lst)(car lst)))
-
gotcha, thanks.
:lol:
this one should be "Wahoo!" instead of "cheesy", I am thinking...
-
?? weird that you would compare the performance of list and append ??
-
8-)
curmudgeon used append to get his result & I used list. Made sense in my little world. :-)
-
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.
-
gile did the "home work" :-)
Thanks
-
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
-
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-)