A list is a group of related values separated by spaces and enclosed in parentheses.
Lists provide an efficient method of storing numerous related values. After all, LISP is so-named because it is the LISt Processing language. Once you understand the power of lists, you will find that you can create more powerful and flexible applications. Lists are used to represent 2D and 3D coordinate values, and entity data.
(setq lst1 (list 1.0 "One" 1)) (1.0 "One" 1)
(setq lst1 '(1.0 "One" 1)) (1.0 "One" 1)
The append function allows you to add new items to the end of a list, and the cons function allows you to add new items to the beginning of a list. The subst function can be used to substitute a new item for every occurrence of an old item. These functions do not modify the original list; they return a modified list. If you need to modify the original list, you explicitly replace the old list with the new list.
The append function takes any number of lists and runs them together as one list. Therefore, all arguments to this function must be lists. The following code adds another "One" to the list stored in lst1.
(setq lst2 (append lst1 '("One"))) (1.0 "One" 1 "One")
(setq lst3 (cons "One" lst2 )) ("One" 1.0 "One" 1 "One")
(setq lst4 (subst "one" "One" lst3)) ("one" 1.0 "one" 1 "one")
You can retrieve a specific item from a list with the nth function. This function accepts two arguments. The first argument is an integer that specifies which item to return. Lists start with a 0 index. A 0 specifies the first item in a list, 1 specifies the second item, and so on. The second argument is the list itself. The following code returns the second item in lst1.
(nth 1 lst1) "One"
(car lst1) 1.0
(cdr lst1) ("One" 1)
AutoLISP also offers a number of additional functions that are variations of the car and cdr functions. For example, the cadr function returns the second element of a list and the caddr function returns the third item of a list. The cadr function is like using the cdr function on a list and then car on the resulting list.
(cadr lst1) "One" (car (cdr lst1)) "One"