M76 Posted March 3, 2010 Posted March 3, 2010 Hi, I'm trying to construct a list of points, but when I try to add a new element to the list with cons, the result is not what I expect. Example: list looks like this: ((50.0 40.0)) I have one point as a list in a list. I try to add to the list with cons for example (20.0 30.0) I get this as a result: (((20.0 30.0)) 50.0 40.0) why? Quote
MSasu Posted March 3, 2010 Posted March 3, 2010 Use APPEND statement instead. (setq List1st '((1 2) (3 4) (5 6) (7 )) (setq List2nd '()) (foreach thePoint List1st (setq List2nd (append List2nd (list thePoint))) ) Regards, Quote
Lee Mac Posted March 3, 2010 Posted March 3, 2010 I'm not sure of the desired result, but cons should return like this: (cons '(20 30) '((40 50))) ==> ((20 30) (40 50)) Quote
David Bethel Posted March 3, 2010 Posted March 3, 2010 I think Lee's example is used lot more ( cons ) is a lot lot faster than ( append ) but it does make the final list in reverse order at times. I use this a lot (setq l1 '(40 50)) (setq l1 (cons '(20 30) l1) (setq l1 (reverse l1)) -David Quote
Lee Mac Posted March 3, 2010 Posted March 3, 2010 Not really a conclusive test, but just as an example: (defun cons-it nil (cons '(40 50) '((20 30)))) (defun append-it nil (append '((40 50)) '((20 30)))) (Benchmark '((cons-it) (append-it))) Benchmarking ...................Elapsed milliseconds / relative speed for 65536 iteration(s): (CONS-IT).......1357 / 1.06 <fastest> (APPEND-IT).....1436 / 1.00 <slowest> Quote
M76 Posted March 3, 2010 Author Posted March 3, 2010 I'm not sure of the desired result, but cons should return like this: (cons '(20 30) '((40 50))) ==> ((20 30) (40 50)) Yes it should but it does not. And I don't know why. I can use append, speed is not an issue now. Quote
Lee Mac Posted March 3, 2010 Posted March 3, 2010 Yes it should but it does not. And I don't know why. What code are you using? 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.