List , What is wrong?

List , What is wrong?

GeryKnee
Advocate Advocate
663 Views
10 Replies
Message 1 of 11

List , What is wrong?

GeryKnee
Advocate
Advocate

What is wrong in the following code?


(defun fListAddPoint ( aPointsList aPoint / )
(if aPointsList
(setq aPointsList(append aPointsList(list aPoint)))
(setq aPointsList(list aPoint))
)
)


(defun c:xx ( /)
(setq L nil)
(if (setq p (getpoint "\nSelect a point: "))
(Progn
(xw p)
(fListAddPoint L p)
)
)
(princ)
)

Thanks,

Gery

0 Likes
Accepted solutions (1)
664 Views
10 Replies
Replies (10)
Message 2 of 11

GeryKnee
Advocate
Advocate

or,

 


(defun c:xx ( /)
(setq L nil)
(if (setq p (getpoint "\nSelect a point: "))
(Progn
;;;;;;;(xw p)
(fListAddPoint L p)
)
)
(princ)
)

0 Likes
Message 3 of 11

Shneuph
Collaborator
Collaborator

What is xw?

---sig---------------------------------------
'(83 104 110 101 117 112 104 64 71 109 97 105 108 46 99 111 109)
0 Likes
Message 4 of 11

GeryKnee
Advocate
Advocate

ignore it

the wrong is that the code is not building the list

in first call the List valuw is nil so must start building

in next calls must rebuild it adding points

Thanks

0 Likes
Message 5 of 11

Shneuph
Collaborator
Collaborator
Accepted solution

Does this do what you want?

(defun fListAddPoint ( aPointsList aPoint / )
(if aPointsList
(setq aPointsList(append aPointsList(list aPoint)))
(setq aPointsList(list aPoint))
)
)


(defun c:xx ( / )
;(setq L nil)
(if (setq p (getpoint "\nSelect a point: "))
(Progn
;(xw p)
  (setq L (fListAddPoint L p))
)
)
(princ)
)
---sig---------------------------------------
'(83 104 110 101 117 112 104 64 71 109 97 105 108 46 99 111 109)
Message 6 of 11

Sea-Haven
Mentor
Mentor

For me

(defun ahaddlstpts ( / )
(setq lst '())
(while (setq pt (getpoint "\nPick point enter to exit"))
(setq lst (cons pt lst))
)
)

I always declare an empty list then just cons to it. 

Message 7 of 11

Shneuph
Collaborator
Collaborator

Should there not be some way of clearing the list through the command when you're ready to start over creating a new list?

 

---sig---------------------------------------
'(83 104 110 101 117 112 104 64 71 109 97 105 108 46 99 111 109)
0 Likes
Message 8 of 11

Kent1Cooper
Consultant
Consultant

@Sea-Haven wrote:

.... I always declare an empty list then just cons to it. 


It really accomplishes nothing to start with an empty list, because an empty list does not exist:

Command: (setq what '())
nil

and if I check for what the list contains, if anything:

Command: !what
nil

[not '() as was originally given to it].

and if I ask for what kind of thing it is:

Command: (type what)
nil
not what you get for one that does exist:
Command: (type VariableContainingList)
LIST

 

It may "look like" a list with nothing in it exists, if you ask for how many items are in it:

Command: (length what)
0

But that's what it returns for any nonexistent list:

Command: (length MarilynMonroe)
0

 

So you can just start a list without its existing ahead of time, by the putting of the first thing into it [here with the 'lst' variable not yet existing], either with (cons):

Command: (setq pt (getpoint))
(16.3829 3.24624 0.0)

Command: (setq lst (cons pt lst))
((16.3829 3.24624 0.0))

or with (append):

Command: (setq lst (append lst (list pt)))
((16.3829 3.24624 0.0))

 

EDIT:  WHICH MEANS that the (fListAddPoint) function in Messages 1 & 5 has no need to check whether the 'aPointsList' variable exists.  Whether it does or not, it can do simply this line:

(setq aPointsList (append aPointsList (list aPoint)))

Kent Cooper, AIA
Message 9 of 11

Sea-Haven
Mentor
Mentor

Agree dont have to worry about if lst exists, but I use this name in 99% of my code so if I forget to localise it and have used it already in another program I will get an incorrect lst returned. ("A" 1 2 3 ) the "A" should only be there.

 

In testing new code I test as I go so often need to jump back to set lst to nil. Else instead of say 4 points in a list I would get 8 12 etc.

 

In a "repeat" it is often necessary to reset the lst a lot of the time. So a new list is made inside the loop. 

0 Likes
Message 10 of 11

Kent1Cooper
Consultant
Consultant

@Sea-Haven wrote:

.... In testing new code I test as I go so often need to jump back to set lst to nil. .... 


(setq lst nil)

[to directly make it non-existent] is just as effective at doing that as

(setq lst '())

is [to make an "empty list," which doesn't exist anyway], with exactly the same result.

 

But they use the same number of code characters, so maybe it doesn't matter....

Kent Cooper, AIA
0 Likes
Message 11 of 11

Kent1Cooper
Consultant
Consultant

@GeryKnee wrote:

(defun c:xx ( /)
(setq L nil)
(if (setq p (getpoint "\nSelect a point: "))
(Progn
;;;;;;;(xw p)
(fListAddPoint L p)
)
)
(princ)
)


I don't think you need the fListAddPoint) function at all.  Would the whole purpose not be served by just this, without any sub-routine?

(defun c:xx (/ p)
  (setq L nil)
  (if (setq p (getpoint "\nSelect a point: "))
    (setq L (append L (list p)))
  )
  (princ)
)

You can use (while) instead of (if) if you want to continue picking points to add to the list, within one running of the command.

Kent Cooper, AIA
0 Likes