# Lesson41 – Advanced List Functions

Another common task is processing lists so you can come up with the correct item to prompt or convert. You should become familiar with as many list functions as possible. These are the fundamental tools for storing, retrieving, and processing information with AutoLISP.

Explanation: List functions

Examples

(setq LISTX ‘(“jan” “feb” “mar” “apr” “may” “jun” “jul” ))
(setq LISTZ ‘(“aug” “sep” “oct” “nov” “dec”))
Command: (reverse LISTX)
(“jul” “jun” “may” “apr” “mar” “feb” “jan”)

Command: (length LISTX)
7

Command: (nth 4 LISTX)
“apr”

Command: (member “apr” LISTX)
(“apr” “may” “jun” “jul”)

Command: (append LISTX LISTZ)
(“jan” “feb” “mar” “apr” “may” “jun” “jul” “aug” “sep” “oct” “nov” “dec”)

PRACTICE

Create a function in a file called EVERY3.LSP that returns a list containing every third element of LISTX using Nth and Length. Test it using the following list:

(setq listx ‘(“jan” “feb” “mar” “apr” “may” “jun” “jul”))

Solution

(defun EVERY3 (LISTX)
(setq COUNT 0)
(while (<= COUNT (length LISTX))
(setq LISTO (cons (nth COUNT LISTX) LISTO))
(setq COUNT (+ COUNT 3))
)
(setq LISTO (reverse LISTO))
)

• A complete solution to this exercise is on your class disk as EVERY3-A.LSP.

Extra Practice

Edit EVERY3.LSP and create EVERYN.LSP, a function that lets you select every nth element. Test it on the following list:

(setq LIST99 ‘(A B C D E F G H I J K L)) (everyn 2 LIST99)

Solution

;** EVERYN – returns a list containing every nth member of
;** an indicated list
(defun EVERYN (N LISTX / COUNT LISTO)
(setq COUNT 0)
(setq LISTO nil)
(while (<= COUNT (length LISTX))
(setq LISTO (cons (nth COUNT LISTX) LISTO))
(setq COUNT (+ COUNT N)) )
(reverse LISTO) )

• A complete solution to this exercise is on your class disk as EVERYN-A.LSP.