samifox Posted May 24, 2013 Posted May 24, 2013 Hi I want simply to add my picked points number to a list and when I press enter a prompt will tell how many members there are in the list (defun C:TEST (/ a lst) (setq lst) (while (setq a (getpoint "Enter a point")) (cons a lst) ) (princ (strcat "Number of points stored :" (itoa(length lst )))) ) running ti give 0. I guess it because cons assumes lst is existing list? Shay Quote
marko_ribar Posted May 24, 2013 Posted May 24, 2013 (defun C:TEST ( / a lst ) (while (setq a (getpoint "Enter a point")) (setq lst (cons a lst)) ) (princ (strcat "\nNumber of points stored : " (itoa (length lst)))) ) Quote
samifox Posted May 24, 2013 Author Posted May 24, 2013 (defun C:TEST ( / a lst ) (while (setq a (getpoint "Enter a point")) [color=red](setq lst (cons a lst))[/color] ) (princ (strcat "\nNumber of points stored : " (itoa (length lst)))) ) what actually happened here? thanks (it worked) Quote
marko_ribar Posted May 24, 2013 Posted May 24, 2013 (setq lst (cons a lst)) This line adds variable a to variable lst always before items that consist variable lst... Since variable lst wasn't setq-ed, lst variable had nil value, and as nil is both an atom and list, variable lst is constructed from empty nil list... So after first while pass, variable lst will contain point data of firstly picked point in form : !lst => ((x1 y1 z1))... When you continue with while loop, lst variable will become more and more filled with data : !lst => ((x3 y3 z3) (x2 y2 z2) (x1 y1 z1))... Every new point data of variable a will be added to previous value of lst, so new variable lst is setq-ed based on previous value of lst, that's why (setq [new value]lst (cons a [previous value]lst))... Every new data of variable a comes in front of previous so when you start calling first in constructed variable lst with (nth 0[means first in list] lst), return value will be last picked point, for ex. if you picked 4 points (nth 0 lst) will return => (x4 y4 z4)... In opposite way you could instead of function (cons) to construct list, used function (append), but operation will differ from cons in a way that new item is appended to lst : - here (setq lst (append (list a) lst)) will append ((x1 y1 z1)) to nil list - starting value of variable lst... This will be the same as (setq lst (cons a lst)) - but here (setq lst (append lst (list a))) will append nil list to ((x1 y1 z1)) value of a... Every next loop will then add previous value of lst to new value of a point data... So this is the opposite from (setq lst (cons a lst)), as with 4 loops new lst variable will look like : ((x1 y1 z1) (x2 y2 z2) (x3 y3 z3) (x4 y4 z4)) - every previous value of lst is added in front - before new (last) a point data... So (nth 0 lst) will then return => (x1 y1 z1) - very first point data you picked when started TEST function... Quote
MSasu Posted May 24, 2013 Posted May 24, 2013 One addition, since by using CONS, the list first entry will be the last added one, you may want to take a look also to REVERSE funntion to be able to parse list's content in the order of definition. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.