Dotted Pairs

AutoCAD AutoLISP & Visual LISP

 
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.

_$ (setq sublist (cons
'lyr "WALLS"))
(LYR . "WALLS") 

The car, cdr, and assoc functions are useful for handling dotted pairs. The following code creates an association list, which is a list of lists, and is the method AutoLISP uses to maintain entity definition data. (Entity definition data is discussed in Using AutoLISP to Manipulate AutoCAD Objects) The following code creates an association list of dotted pairs:

_$ (setq wallinfo (list
sublist(cons 'len 240.0) (cons 'hgt 96.0)))
( (LYR . "WALLS")  (LEN . 240.0)  (HGT . 96.0) ) 

The assoc function returns a specified list from within an association list regardless of the specified list's location within the association list. The assoc function searches for a specified key element in the lists, as follows:

_$  (assoc 'len wallinfo)
(LEN  .  240.0)
_$  (cdr (assoc 'lyr
wallinfo))
"WALLS"
_$  (nth 1 wallinfo)
(LEN  .  240.0)
_$  (car (nth 1 wallinfo))
LEN