Skip to content

AutoLISP (2 of 5)

<=

(<= value value...)

T if each value is less than or equal to the next one.

(<= 1 1 2)   ; T

Accepts numbers and strings, like <. With more than two values it checks that they never decrease: equal values in a row are fine, a value smaller than the previous one is not.

Example

A counter that must not go beyond 10:

(setq n 10)
(<= 0 n 10)   ; T

See also

< · >= · =

>=

(>= value value...)

T if each value is greater than or equal to the next one.

(>= 2 2 1)   ; T

Accepts numbers and strings, like <. With more than two values it checks that they never increase: equal values in a row are fine. It is handy to validate typed data before using it.

Example

Accept only positive or zero distances:

(setq d 0)
(>= d 0)   ; T

See also

> · <= · minusp

eq

(eq expression expression)

T if both are the same object: the same symbol, the same number or the same list (not an equal copy). To compare contents, use equal.

(setq a '(1 2) b a)
(eq a b)          ; T
(eq a '(1 2))     ; nil

equal

(equal expression expression [fuzz])

T if both have the same contents. With a fuzz, numbers may differ by up to that amount (useful to compare points).

(equal '(1 2) '(1 2))                  ; T
(equal '(1.0 2.0) '(1.0 2.0001) 0.001) ; T

not

(not expression)

T if the expression is nil; nil otherwise. Same as null.

(not nil)   ; T

Any value that is not nil counts as true, including 0 and the empty string: (not 0) gives nil. It is usually used with predicates ((not (boundp 'x))) and null with lists, although they do the same.

Example

Repeat while nothing has been selected:

(while (not (setq e (entsel "\nSelect an object: "))))

See also

null · and · or

null

(null expression)

T if the expression is nil (or the empty list).

(null '())   ; T

Only nil gives T: 0 and the empty string are not nil ((null 0) gives nil). Use it to know whether a list is already empty while walking it.

Example

Count the elements of a list without length:

(defun count-items (l) (if (null l) 0 (1+ (count-items (cdr l)))))
(count-items '(a b c))   ; 3

See also

not · listp · boundp

atom

(atom expression)

T if the expression is not a list with elements (nil is an atom too).

(atom 'a)      ; T
(atom '(1))    ; nil

Everything that is not a list with elements is an atom: numbers, strings, symbols, entity names and nil ((atom nil) gives T). It is the opposite of vl-consp.

Example

Walk a nested list down to its atoms:

(defun leaves (x) (if (atom x) 1 (+ (leaves (car x)) (if (cdr x) (leaves (cdr x)) 0))))
(leaves '(1 (2 3) 4))   ; 4

See also

listp · vl-consp · type

listp

(listp expression)

T if the expression is a list or nil.

(listp nil)   ; T

Unlike vl-consp, it also gives T with nil, because nil is the empty list. A number or a string gives nil.

Example

(listp '(1))   ; T
(listp 1)      ; nil

What it is for

It checks whether a value is a list before treating it as one with car, nth or foreach. For example, getpoint returns a list (the point), a string if a keyword was typed or nil if Enter was pressed.

More examples

(listp "a")        ; nil
(listp '(1 . 2))   ; T: a dotted pair is also a list
(vl-consp nil)     ; nil
(vl-consp '(1))    ; T

Common mistakes

See also

vl-consp · atom · type

vl-consp

(vl-consp expression)

T if the expression is a list with elements (not nil).

(vl-consp nil)    ; nil
(vl-consp '(1))   ; T

Unlike listp, it gives nil with nil. Use it to know whether a value has a car before asking for it, for example when walking nested lists or dotted pairs.

Example

(vl-consp '(8 . "0"))   ; T
(vl-consp "0")          ; nil

See also

listp · atom · car

numberp

(numberp expression)

T if the expression is an integer or a real.

(numberp 3.5)   ; T

A string with digits is not a number: (numberp "3") gives nil; convert it first with atoi or atof. Use it to check data before computing.

Example

Keep the numbers of a list:

(vl-remove-if-not 'numberp '(1 "a" 2))   ; (1 2)

See also

type · zerop · atof

zerop

(zerop number)

T if the number is 0.

(zerop 0.0)   ; T

It compares exactly with 0: a very small real is not zero ((zerop 1e-20) gives nil). For results of computations use a tolerance with equal.

Example

(zerop (- 5 5))                   ; T
(equal (- 0.3 0.1 0.2) 0.0 1e-9)  ; T

See also

minusp · equal · =

minusp

(minusp number)

T if the number is negative.

(minusp -1)   ; T

Zero is not negative: (minusp 0) gives nil. An argument that is not a number gives bad argument type: numberp. Use it with vl-remove-if to drop the negative values of a list.

Example

(vl-remove-if 'minusp '(1 -2 3))   ; (1 3)

See also

zerop · abs · <

boundp

(boundp symbol)

T if the symbol has a value other than nil.

(boundp 'pi)   ; T

A symbol set to nil counts as having no value: after (setq empty nil), (boundp 'empty) gives nil. Use it to know whether a routine is already loaded ((boundp 'c:number)) or whether a global variable has a value before using it.

Example

Give a default value the first time:

(if (not (boundp 'radius)) (setq radius 5.0))

See also

setq · null · vl-symbol-value

type

(type expression)

Type of the expression as a symbol: INT, REAL, STR, SYM, LIST, SUBR (built-in function), USUBR (user function), ENAME, PICKSET, FILE or VL-CATCH-ALL-APPLY-ERROR. For nil, nil.

(type 1.5)     ; REAL
(type "a")     ; STR

vl-symbolp

(vl-symbolp expression)

T if the expression is a symbol (nil is not).

(vl-symbolp 'a)   ; T

Strings and numbers are not symbols ((vl-symbolp "a") gives nil), and neither is nil.

Example

(vl-symbolp 'a)    ; T
(vl-symbolp nil)   ; nil

See also

type · vl-symbol-name · atom

car

(car list)

First element of the list. For nil, nil. In a dotted pair, the first part.

(car '(1 2 3))   ; 1
(car '(8 . "0")) ; 8

With entity data (entget), car gives the group code of each pair. (car nil) gives nil, but a number or another atom gives bad argument type: consp. Combinations such as cadr or caddr chain car and cdr.

Example

X coordinate of a point:

(setq p '(12.5 3.0 0.0))
(car p)   ; 12.5

See also

cdr · cadr · nth

cdr

(cdr list)

The list without its first element. In a dotted pair, the second part.

(cdr '(1 2 3))    ; (2 3)
(cdr '(8 . "0"))  ; "0"

With a dotted pair from entget it gives the value of the group code: it is how the layer or the radius are read. (cdr '(1)) gives nil. Together with assoc it is the most common way to read entity data.

Example

Type of the last entity created:

(command "_circle" '(0 0) 5)
(cdr (assoc 0 (entget (entlast))))   ; "CIRCLE"

See also

car · assoc · cddr

caar

(caar list)

First element of the first element: the first number of a point at the start of the list. Same as (car (car list)). If any step is not a list, it is an error.

(caar list)   ; = (car (car list))

caaar

(caaar list)

A combination of car and cdr: the letters between c and r are applied from right to left (a = car, d = cdr). Same as (car (car (car list))). If any step is not a list, it is an error.

(caaar list)   ; = (car (car (car list)))

cdaar

(cdaar list)

A combination of car and cdr: the letters between c and r are applied from right to left (a = car, d = cdr). Same as (cdr (car (car list))). If any step is not a list, it is an error.

(cdaar list)   ; = (cdr (car (car list)))

caaaar

(caaaar list)

A combination of car and cdr: the letters between c and r are applied from right to left (a = car, d = cdr). Same as (car (car (car (car list)))). If any step is not a list, it is an error.

(caaaar list)   ; = (car (car (car (car list))))

cadaar

(cadaar list)

A combination of car and cdr: the letters between c and r are applied from right to left (a = car, d = cdr). Same as (car (cdr (car (car list)))). If any step is not a list, it is an error.

(cadaar list)   ; = (car (cdr (car (car list))))

cdaaar

(cdaaar list)

A combination of car and cdr: the letters between c and r are applied from right to left (a = car, d = cdr). Same as (cdr (car (car (car list)))). If any step is not a list, it is an error.

(cdaaar list)   ; = (cdr (car (car (car list))))

cddaar

(cddaar list)

A combination of car and cdr: the letters between c and r are applied from right to left (a = car, d = cdr). Same as (cdr (cdr (car (car list)))). If any step is not a list, it is an error.

(cddaar list)   ; = (cdr (cdr (car (car list))))

cadr

(cadr list)

Second element of the list. Of a point, the Y coordinate. Same as (car (cdr list)). If any step is not a list, it is an error.

(cadr list)   ; = (car (cdr list))

caadr

(caadr list)

A combination of car and cdr: the letters between c and r are applied from right to left (a = car, d = cdr). Same as (car (car (cdr list))). If any step is not a list, it is an error.

(caadr list)   ; = (car (car (cdr list)))

cdadr

(cdadr list)

A combination of car and cdr: the letters between c and r are applied from right to left (a = car, d = cdr). Same as (cdr (car (cdr list))). If any step is not a list, it is an error.

(cdadr list)   ; = (cdr (car (cdr list)))

caaadr

(caaadr list)

A combination of car and cdr: the letters between c and r are applied from right to left (a = car, d = cdr). Same as (car (car (car (cdr list)))). If any step is not a list, it is an error.

(caaadr list)   ; = (car (car (car (cdr list))))

cadadr

(cadadr list)

A combination of car and cdr: the letters between c and r are applied from right to left (a = car, d = cdr). Same as (car (cdr (car (cdr list)))). If any step is not a list, it is an error.

(cadadr list)   ; = (car (cdr (car (cdr list))))

cdaadr

(cdaadr list)

A combination of car and cdr: the letters between c and r are applied from right to left (a = car, d = cdr). Same as (cdr (car (car (cdr list)))). If any step is not a list, it is an error.

(cdaadr list)   ; = (cdr (car (car (cdr list))))

cddadr

(cddadr list)

A combination of car and cdr: the letters between c and r are applied from right to left (a = car, d = cdr). Same as (cdr (cdr (car (cdr list)))). If any step is not a list, it is an error.

(cddadr list)   ; = (cdr (cdr (car (cdr list))))

cdar

(cdar list)

The first element without its first element. Same as (cdr (car list)). If any step is not a list, it is an error.

It is one of the composed list accessors that save nesting car and cdr by hand. For a dotted pair such as (8 . "WALLS"), the cdr is the value, so (cdar lst) reads the value of the first pair of an association list.

(cdar list)   ; = (cdr (car list))

cadar

(cadar list)

Second element of the first element. Same as (car (cdr (car list))). If any step is not a list, it is an error.

It is one of the composed list accessors (caar, cadr, cdar, cddr, cadar and so on) that save nesting car and cdr by hand. It is common on lists of lists, such as the result of entget, where each item is a dotted pair.

(cadar list)   ; = (car (cdr (car list)))

cddar

(cddar list)

A combination of car and cdr: the letters between c and r are applied from right to left (a = car, d = cdr). Same as (cdr (cdr (car list))). If any step is not a list, it is an error.

(cddar list)   ; = (cdr (cdr (car list)))

caadar

(caadar list)

A combination of car and cdr: the letters between c and r are applied from right to left (a = car, d = cdr). Same as (car (car (cdr (car list)))). If any step is not a list, it is an error.

(caadar list)   ; = (car (car (cdr (car list))))

caddar

(caddar list)

A combination of car and cdr: the letters between c and r are applied from right to left (a = car, d = cdr). Same as (car (cdr (cdr (car list)))). If any step is not a list, it is an error.

(caddar list)   ; = (car (cdr (cdr (car list))))

cdadar

(cdadar list)

A combination of car and cdr: the letters between c and r are applied from right to left (a = car, d = cdr). Same as (cdr (car (cdr (car list)))). If any step is not a list, it is an error.

(cdadar list)   ; = (cdr (car (cdr (car list))))

cdddar

(cdddar list)

A combination of car and cdr: the letters between c and r are applied from right to left (a = car, d = cdr). Same as (cdr (cdr (cdr (car list)))). If any step is not a list, it is an error.

(cdddar list)   ; = (cdr (cdr (cdr (car list))))

cddr

(cddr list)

The list without its first two elements. Same as (cdr (cdr list)). If any step is not a list, it is an error.

Typical use: skipping the first two items of a list before walking the rest with foreach or mapcar. Applied to a list with fewer than two items it returns nil.

(cddr list)   ; = (cdr (cdr list))

caddr

(caddr list)

Third element of the list. Of a 3D point, the Z coordinate. Same as (car (cdr (cdr list))). If any step is not a list, it is an error.

(caddr list)   ; = (car (cdr (cdr list)))

cdddr

(cdddr list)

The list without its first three elements. Same as (cdr (cdr (cdr list))). If any step is not a list, it is an error.

(cdddr list)   ; = (cdr (cdr (cdr list)))

caaddr

(caaddr list)

A combination of car and cdr: the letters between c and r are applied from right to left (a = car, d = cdr). Same as (car (car (cdr (cdr list)))). If any step is not a list, it is an error.

(caaddr list)   ; = (car (car (cdr (cdr list))))

cadddr

(cadddr list)

Fourth element of the list. Same as (car (cdr (cdr (cdr list)))). If any step is not a list, it is an error.

(cadddr list)   ; = (car (cdr (cdr (cdr list))))

cdaddr

(cdaddr list)

A combination of car and cdr: the letters between c and r are applied from right to left (a = car, d = cdr). Same as (cdr (car (cdr (cdr list)))). If any step is not a list, it is an error.

(cdaddr list)   ; = (cdr (car (cdr (cdr list))))

cddddr

(cddddr list)

A combination of car and cdr: the letters between c and r are applied from right to left (a = car, d = cdr). Same as (cdr (cdr (cdr (cdr list)))). If any step is not a list, it is an error.

(cddddr list)   ; = (cdr (cdr (cdr (cdr list))))

cons

(cons element list-or-atom)

With a list, returns it with the element in front. With an atom, creates a dotted pair, like those of entget.

(cons 0 '(1 2))   ; (0 1 2)
(cons 8 "Walls")  ; (8 . "Walls")

What it is for

It is the basic way to build lists in AutoLISP. It is used mainly for two things: creating the DXF pairs required by entmake, entmod and ssget filters, and collecting results in a loop.

More examples

(cons '(1 2) '(3))           ; ((1 2) 3)
(cons 10 '(0.0 5.0 0.0))     ; (10 0.0 5.0 0.0), a point with its code
(cons 1 nil)                 ; (1)

Example

Change the layer in an object's list (as is done before entmod):

(setq e '((0 . "LINE") (8 . "0")))
(subst (cons 8 "AXES") (assoc 8 e) e)   ; ((0 . "LINE") (8 . "AXES"))

Collecting in a loop: each cons puts the element in front, so at the end the list is reversed with reverse:

(setq l nil)
(setq l (cons 3 l))
(setq l (cons 2 l))   ; (2 3)
(reverse l)           ; (3 2)

Common mistakes

See also

list · append · subst · entmod

list

(list [expression]...)

List with the values of the expressions. With no arguments, nil.

(list 1 "a" 2.5)   ; (1 "a" 2.5)

Unlike quote, it evaluates its arguments: (list 'a (+ 1 2)) gives (A 3). It is the way to build a point from computed coordinates.

Example

Midpoint of two points:

(setq a '(0 0) b '(10 4))
(list (/ (+ (car a) (car b)) 2.0) (/ (+ (cadr a) (cadr b)) 2.0))   ; (5.0 2.0)

See also

cons · append · quote

append

(append [list]...)

Joins the lists into one.

(append '(1 2) '(3) '(4 5))   ; (1 2 3 4 5)

The lists are not modified: a new one is returned. With no arguments it gives nil. Each argument must be a list; to add a single element, wrap it in a list, or use cons if it goes at the front.

Example

Add a point at the end of a list of vertices:

(setq pts '((0 0) (10 0)))
(setq pts (append pts (list '(10 5))))   ; ((0 0) (10 0) (10 5))

See also

cons · list · reverse

reverse

(reverse list)

The list with its elements in reverse order.

(reverse '(1 2 3))   ; (3 2 1)

(reverse nil) gives nil. The original list does not change. It is common when building a list with cons, which adds at the front: at the end the list is reversed.

Example

Build a list in order:

(setq l nil)
(foreach x '(1 2 3) (setq l (cons (* x x) l)))
(reverse l)   ; (1 4 9)

See also

cons · append · last

length

(length list)

Number of elements of the list.

(length '(1 2 3))   ; 3

(length nil) gives 0. It only counts the first level: a list of points gives the number of points. For selection sets use sslength.

Example

Index of the last element:

(setq l '(a b c))
(1- (length l))   ; 2

See also

vl-list-length · sslength · nth

vl-list-length

(vl-list-length list)

Number of elements of the list, same as length.

(vl-list-length '(a b))   ; 2

With a dotted pair it does not fail: it counts the elements before the dot ((vl-list-length '(1 . 2)) gives 1).

Example

(vl-list-length '((0 0) (10 0) (10 5)))   ; 3

See also

length · vl-consp

nth

(nth index list)

Element at the given position, starting at 0. Outside the list, nil.

(nth 1 '(a b c))   ; B

A negative index or one beyond the list also gives nil. For the first and second elements car and cadr are shorter.

Example

Z coordinate of a point:

(nth 2 '(10.0 5.0 3.5))   ; 3.5

See also

car · last · vl-position

member

(member expression list)

Looks for the expression in the list (with equal) and returns the rest of the list from it, or nil if it is not there.

(member 2 '(1 2 3))   ; (2 3)

assoc

(assoc key list)

In a list of pairs, the first one whose first part is the key (with equal), or nil. It is the way to read a code from an entget list.

(cdr (assoc 8 (entget (entlast))))   ; layer name

subst

(subst new old list)

Copy of the list with every element equal to old replaced by new. Used to change a pair of an entget list before entmod.

(subst '(8 . "New") (assoc 8 e) e)

Differences with AutoCAD

Only the elements of the top level of the list are replaced, not those inside sublists.

last

(last list)

Last element of the list.

(last '(1 2 3))   ; 3

For an empty list, nil. If the last element is a list, it returns it whole ((last '((1 2) (3 4))) gives (3 4)). It is handy for the last vertex of a list of points.

Example

First and last point of a path:

(setq pts '((0 0) (5 0) (5 5)))
(list (car pts) (last pts))   ; ((0 0) (5 5))

See also

car · nth · reverse

vl-remove

(vl-remove element list)

The list without the elements equal to the given one.

(vl-remove 2 '(1 2 3 2))   ; (1 3)

It compares with equal: strings are case-sensitive ((vl-remove "a" '("a" "b" "A")) gives ("b" "A")). The original list does not change.

Example

Remove layer 0 from a list of layers:

(vl-remove "0" '("0" "WALLS" "DIMS"))   ; ("WALLS" "DIMS")

See also

vl-remove-if · subst · member

vl-position

(vl-position element list)

Position (from 0) of the first occurrence of the element, or nil.

(vl-position "b" '("a" "b"))   ; 1

It compares with equal, so it also finds lists and points: (vl-position '(1 2) '(0 (1 2))) gives 1. Strings are case-sensitive.

Example

Position of a layer in a list:

(vl-position "DOORS" '("WALLS" "DOORS" "DIMS"))   ; 1
(vl-position 9 '(1 2))                            ; nil

See also

member · nth · vl-string-position

Download LinceCAD Manual contents

Download the free 30-day trial · Open and edit DWG files · AutoCAD LT alternative

Coming from AutoCAD? Check what is compatible · Linux CAD with native DWG