Satish Lele
satish.lele@gmail.com


List Handling
Creating a list: You can create a list by list operator or ’.
(list a1 a2 a3 a4)
’(a1 a2 a3 a4)
It takes any number of expressions or lists, and combines them into one list. (list expr...)
(list 'a 'b 'c) returns (A B C)
(list 'a '(b c) 'd) returns (A (B C) D)
(list 3.9 6.7) returns (3.9 6.7)

Quote: The single quote character ( ' ) is defined as the quote function. '(3.9 6.7) means the same as (list 3.9 6.7). This can be useful for creating association lists and defining points. It does not allow the evaluation of its arguments.

Point Lists: AutoLISP observes the following conventions for handling graphics coordinates. Points are expressed as lists of two or three numbers surrounded by parentheses. 2D points Expressed as lists of two real numbers (X and Y, respectively), as in (setq p1 (list 3.4 7.52)). 3D points Expressed as lists of three real numbers (X, Y, and Z, respectively), as in (setq p1 (list 3.4 7.52 1.0))
In AutoLISP, this function is frequently used to define a 2D or 3D point variable (a list of two or three reals). As an alternative to using the list function, you can explicitly quote a list with the quote function if there are no variables or undefined items in the list.

append: Takes any number of lists and runs them together as one list. (append list ...).
(append '(a b) '(c d)) ;returns (A B C D)
(append '((a)(b)) '((c)(d))) ;returns ((A)(B)(C)(D))

cons: The basic list constructor. (cons new-first-element list)
The cons function takes an element (new-first-element) and a list, and returns the addition of that element to the beginning of the list. The first element can be an atom or a list.
(cons 'a '(b c d)) returns (A B C D)
(cons '(a) '(b c d)) returns ((A) B C D)
cons function also accepts an atom in place of the list argument, constructing a structure known as a dotted pair. When displaying a dotted pair, AutoLISP prints a period, or dot, between its first and second elements. You can use the cdr function to return the second atom of a dotted pair.
(cons 'a 2) returns (A . 2)

Dotted Pairs: Another way AutoLISP uses lists to organize data is with a special type of list called a dotted pair. This list must always contain two members. When representing a dotted pair, AutoLISP separates the members of the list with a period (.). Most list-handling functions will not accept a dotted pair as an argument, so you should be sure you are passing the right kind of list to a function.
Dotted pairs are an example of an "improper list." An improper list is one in which the last cdr is not nil. In addition to adding an item to the beginning of a list, the cons function can create a dotted pair. If the second argument to the cons function is anything other than another list or nil, it creates a dotted pair.

reverse: Returns a list with its elements reversed. (reverse lst)
(reverse '((a) b c)) ;returns (C B (A))

last: Returns the last element in a list. (last lst)
(last '(a b c d e)) returns E
(last '(a b c (d e))) returns (D E)
As shown, last can return an atom or a list. At first glance, last seems a perfect way to obtain the Y coordinate of a point. While this is true for 2D points (lists of two reals), last will return the Z coordinate of a 3D point. To let functions work properly with 2D or 3D points, use cadr to obtain Y coordinates, and caddr to obtain Z coordinates.

length: Returns an integer indicating the number of elements in a list (length lst)
(length '(a b c d)) returns 4
(length '(a b (c d))) returns 3
(length '()) returns 0
Generally used with repeat.

member: Searches a list for an occurrence of an expression and returns the remainder of the list, starting with the first occurrence of the expression. (member expr lst)
If there is no occurrence of expr in lst, member returns nil.
(member 'c '(a b c d e)) ;returns (C D E)
(member 'q '(a b c d e)) ;returns nil

nth: Returns the nth element of a list. (nth n lst). n argument is the numeric position of the element to return (zero is the first element). If n is greater than lst's highest element number, nth returns nil. First item in list has counter 0.
(nth 3 '(a b c d e)) returns D
(nth 0 '(a b c d e)) returns A
(nth 5 '(a b c d e)) returns nil

car: Returns the first element of a list. (car list)
If list is empty, car returns nil.
(car '(a b c)) returns A
(car '((a b) c)) returns (A B)
(car '()) returns nil

cadr: Returns the second element of a list. (cadr list).
If list is empty, cadr returns nil.
(cadr '(a b c)) returns b
(cadr '((a b) c)) returns c
(cadr '()) returns nil

caddr: Returns the third element of a list. (caddr list)
If list is empty, caddr returns nil.
(caddr '(a b c)) returns c
(caddr '((a b) c d)) returns d
(caddr '()) returns nil

mapcar: You can use it when you want to apply a function to a list, one argument at a time. It returns a list of the result of executing a function with the individual elements of a list or lists supplied as arguments to the function
(mapcar function list1... listn)
The number of lists must match the number of arguments required by function.
(setq a 10 b 20 c 30)
(mapcar '1+ (list a b c)) returns (11 21 31) is equivalent to (1+ a) (1+ b) (1+ c) except that mapcar returns a list of the results.

apply: It is similar to mapcar. It passes a list of arguments to a specified function.
(apply 'function list)
The apply function works with both built-in functions (subrs) and user-defined functions (those created with either defun or lambda).
(apply '+ '(1 2 3)) returns 6
(apply 'strcat '("a" "b" "c")) returns "abc"

foreach: Evaluates expressions for all members of a list
(foreach name lst expr...)
Steps through lst, assigning each element to name, and evaluates each expr for every element in the list. Any number of exprs can be specified. The foreach function returns the result of the last expr evaluated.
(foreach n '(a b c) (print n)) is equivalent to
(print a)
(print b)
(print c) and returns c,
except that foreach returns the result of only the last expression evaluated.

eval: It forces an extra evaluation of variable item to extract a value. It can be used to find the value of a nested symbol, whose value is also symbol.
(eval expr)
For example, given the assignments
(setq a 123)
(setq b a)
then
(eval 4.0)     returns 4.0
(eval (abs -10))     returns 10
(eval a)     returns 123
(eval b)     returns 123

type: Returns the type of a specified item. (type item). The types are returned as one of the atoms shown in the following table:
Symbol     types
Type     Description
REAL     Floating-point numbers
FILE     File descriptors
STR     Strings
INT     Integers
SYM     Symbols
LIST     Lists (and user functions)
SUBR     Internal functions
EXSUBR External functions (ARX)
PICKSET     Selection sets
ENAME     Entity names
PAGETB     Function paging table

acad_strlsort: Sorts a list of strings by alphabetical order
(acad _strlsort list)
The list argument is the list of strings to be sorted. The acad_strlsort function returns a list of the same strings in alphabetical order. If the list argument list is invalid or if there isn't enough memory to do the sort, acad_strlsort returns nil.
The following code sorts the list of abbreviated month names:
(setq mos '("Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"))
(acad_strlsort mos)
It returns the following list:
("Apr" "Aug" "Dec" "Feb" "Jan" "Jul" "Jun" "Mar" "May" "Nov" "Oct" "Sep")
backBack top