Jump to content

cons list of points?


M76

Recommended Posts

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?

Link to comment
Share on other sites

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,

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...