List in LISP created with elements entered by the user

List in LISP created with elements entered by the user

Anonymous
Not applicable
3,527 Views
4 Replies
Message 1 of 5

List in LISP created with elements entered by the user

Anonymous
Not applicable

I recently started my studies in LISP and am trying to create a program where the user is asked to put the elements to create a list, but I only understood how to create a list with predetermined values like those shown below:

 

(defun testlist()
   (list 1 2 3 4 5)
)  

 If anyone can help me I would appreciate it very much. I really do not understand how to create a multi-valued list that the user will type.

 

note: sorry for my bad english

3,528 Views
4 Replies
Replies (4)
Message 2 of 5

john.uhden
Mentor
Mentor

Your English looks better than many others'.  I like that you are interested in lists because that is the foundation of AutoLisp.  Except for Visual Lisp, almost everything in AutoLisp involves lists.

 

Yes, you can create an explicit list, as in (setq mylist '(1 2 3 4 5)) which is the same as (list 1 2 3 4 5), but not necessarily with everything inside the list.

But you can also build lists in pieces, either one by one or in chunks using the cons and append functions.

Say you have an empty list, like (setq x nil).  You can keep adding items to the front of the list by, for example,

(setq x (cons 1 x))

which them makes x a list of one item, 1, so that !x looks llike (1).

Do it again...

(setq x (cons 2 x))

which turns x into a list of two items looking like (2 1).

You can do this as many times as you have memory for it, which with today's computers is relatively limitless compared to the actual lists you will ever need to create in AutoLisp.

So then let's suppose you create another list (by whatever means), say (setq y (list 4 3 5)).

You can then combine x and y using the append function, as in...

(setq xy (append x y)) which will result in '(2 1 4 3 5).

 

Then there's all the fun with functions like mapcar, subst, nth, member, apply, and the newer Visual Lisp functions like for sorting, removing, and retrieving positions.

F'rinstance (vl-sort xy '<) returns '(1 2 3 4 5)

 

And then there are lists of lists with which you can do all kinds of magical things.

Hey, I'm just a hanger-outer here because I have nothing better to do.  Keep asking your questions and you'll learn a bundle when the big boys step in like @_gile, or @Kent1Cooper, or @ВeekeeCZ, or @Ranjit_Singh, or @dbroad, and a bunch of other wizards.  We all love to help people learn to help themselves.

John F. Uhden

Message 3 of 5

Kent1Cooper
Consultant
Consultant

You can also build one with User inputs directly as that input is provided:

 

(setq mylist

  (list

    (getint "\nInteger to include in the list: ")

    (getreal "\real number to include in the list: ")

    (getstring "\nText string to include in the list: ")

    (car (entsel "\nSelected object to include in the list: "))
  ); list

); setq

 

Often that is not a great idea, because if inappropriate input is supplied, or none, it can cause errors that will spoil the whole thing.  User input can be checked before including it in such a list, in this case checked merely for having been supplied:

 

(if (setq temp (getint "\nInteger to include in the list: "))

  (setq mylist (cons temp mylist))

)

(if (setq temp (getreal "\nReal number to include in the list: "))

  (setq mylist (cons temp mylist))

)

... etc, ...

Kent Cooper, AIA
Message 4 of 5

dbroad
Mentor
Mentor

If you narrow the scope by providing an example of user input, you would get more specifically useful answers if the responses by @john.uhden and @Kent1Cooper are not enough.

Architect, Registered NC, VA, SC, & GA.
0 Likes
Message 5 of 5

Anonymous
Not applicable

Thank you for this explanation, it help me a lot.

Regards

André

0 Likes