The AutoLisp Intermediate Tutorial
List Functions :
car cdr cadr caddr caar cddr foreach list cons nth
You are about to unravel the mystery behind the car of a cdr. Better sit down.
car - This function returns the first item of a list. The item can be a list as shown in example 3 below.
Syntax : (car list)
list - Any valid list.
(car (1 2 3)) returns 1
(car ("ab" "cde" "Jeff")) returns "ab"
(car ((1 2 3) (4 5 6))) returns (1 2 3)
(car 1) returns "Error: Bad Argument Type" [1 is an integer, not a list]
(car "Jeff") returns "Error: Bad Argument Type" ["Jeff" is a string, not a list]
(car (1 (2 3))) returns 1
(car ((1 2) 3)) returns (1 2)
This function is mainly used to get the x coordinate of a point. [ (car (x y z)) returns x ]
cdr - This function returns a list that includes everything but the first item in a list. The item can be a list as shown in example 3 below.
Syntax : (cdr list)
list - Any valid list.
(cdr (1 2 3)) returns ( 2 3)
(cdr ("ab" "cde" "Jeff")) returns ("cde" "Jeff")
(cdr ((1 2 3) (4 5 6))) returns (4 5 6) since (4 5 6) is the second item in the list.
(cdr 1) returns "Error: Bad Argument Type" [1 is an integer, not a list]
(cdr "Jeff") returns "Error: Bad Argument Type" ["Jeff" is a string, not a list]
(cdr (1 (2 3))) returns (2 3)
(cdr ((1 2) 3)) returns 3
Mystery HINT: (car (cdr ( 1 (2 3) ) ) ) returns 2 [So, that's what a car of a cdr is]
if the cdr of (1 (2 3)) returns (2 3) and the car of (2 3) returns 2 then the mystery is solved.
The car of a cdr is the first item in the second item in the list.
cadr - This function returns the second item in a list. The item can be a list as shown in example 3 below.
Syntax : (cadr list)
list - Any valid list.
(cadr (1 2 3)) returns 2
(cadr ("ab" "cde" "Jeff")) returns "cde"
(cadr ((1 2 3) (4 5 6) (7 8 9))) returns (4 5 6)
(cadr 1) returns "Error: Bad Argument Type" [1 is an integer, not a list]
(cadr "Jeff") returns "Error: Bad Argument Type" ["Jeff" is a string, not a list]
(cadr (1 (2 3))) returns (2 3)
(cadr ((1 2) 3)) returns 3
(cadr (1)) returns nil [There isn't a second item]
This function is mainly used to get the y coordinate of a point. [ (cadr (x y z)) returns y ]
caddr - This function returns the third item in a list. The item can be a list as shown in example 3 below.
Syntax : (caddr list)
list - Any valid list.
(caddr (1 2 3)) returns 3
(caddr ("ab" "cde" "Jeff")) returns "Jeff"
(caddr ((1 2 3) (4 5 6) (7 8 9))) returns (7 8 9)
(caddr 1) returns "Error: Bad Argument Type" [1 is an integer, not a list]
(caddr "Jeff") returns "Error: Bad Argument Type" ["Jeff" is a string, not a list]
(caddr (1 (2 3))) returns nil [There isn't a third item]
(caddr ((1 2) 3 (5 6) 7)) returns (5 6)
(caddr (1)) returns nil [There isn't a third item]
This function is mainly used to get the z coordinate of a point. [ (caddr (x y z)) returns z ]
caar - This function returns the first item of the first item in a list. The item can be a list as shown in example 3 below.
Syntax : (caar list)
list - Any valid list.
(caar (1 2 3)) returns "Error: Bad Argument Type" [The first item in the list is not a list]
(caar ((1 2 3)(4 5 6)) returns 1
(caar (("ab" "cde") ("Jeff")) returns "ab"
(caar (("Jeff" 1 2 3)("x" "y" "z")(4 5 6))) returns "Jeff"
(caar 1) returns "Error: Bad Argument Type" [1 is an integer, not a list]
(caar "Jeff") returns "Error: Bad Argument Type" ["Jeff" is a string, not a list]
(caar (1 (2 3))) returns "Error: Bad Argument Type" [The first item in the list is not a list]
(caar ((1 2) 3)) returns 1
cddr - This function returns a list that includes everything after the second item in a list.
Syntax : (cddr list)
list - Any valid list.
(cddr (1 2 3)) returns (3)
(cddr ("ab" "cde" "Jeff" "Sanders")) returns ("Jeff" "Sanders")
(cddr ((1 2 3) (4 5 6) (7 8 9)(10 11 12))) returns ((7 8 9)(10 11 12))
(cddr 1) returns "Error: Bad Argument Type" [1 is an integer, not a list]
(cddr "Jeff") returns "Error: Bad Argument Type" ["Jeff" is a string, not a list]
(cddr (1 (2 3))) returns nil [There isn't a third item]
(cddr ((1 2) 3 (5 6) 7)) returns ((5 6) 7)
(cddr (1)) returns nil [There isn't a third item]
foreach - This function steps through each item in the list and returns the last value.
Syntax : (foreach varName list yourFunctions)
varName - Any valid variable name that you make up.
list - Any valid list.
yourFunctions - Any valid autolisp functions.
(foreach a (list 1 2 3)(princ a)) prints 123 to the screen and returns 3
[same as (princ 1) (princ 2) (princ 3) except that it only returns the last value.]
(foreach a (list 1 2 3)(princ (+ a 5))) prints 678 to the screen and returns 8
[same as (princ (+ 1 5)) (princ (+ 2 5)) (princ (+ 3 5)) except that it only returns the last value.]
list - This function creates a list.
Syntax : (list Item)
Syntax : (list firstItem secondItem)
Syntax : (list firstItem secondItem thirdItem...)
Item - Any valid item including a list.
(list) returns nil [an Empty list]
(list 1 2 3) returns (1 2 3)
(list "ab" "cde" "Jeff" "Sanders") returns ("ab" "cde" "Jeff" "Sanders")
(list (list 1 2 3) (list 4 5 6)) returns ((1 2 3) (4 5 6))
(list 1) returns (1)
(list "Jeff") returns ("Jeff")
(list 1 (list 2 3)) returns (1 (2 3))
(list (list 1 2) 3 (list 5 6) 7) returns ( (1 2) 3 (5 6) 7)
cons - This function takes an item and a list, and returns the addition of that item to the beginning of the list. The first item can be an atom or a list.
Syntax : (list Item list)
Item - Any valid item including a list.
list - Any valid list.
(cons) returns "Error: Too Few Arguments" [cons requires an item and a list]
(cons 1 2 3) returns "Error: Too Many Arguments" [cons requires an item and a list]
(cons "ab" (list "cde" "Jeff" "Sanders")) returns ("ab" "cde" "Jeff" "Sanders")
(cons (list 1 2 3) (list 4 5 6)) returns ((1 2 3) 4 5 6)
(cons 1) returns "Error: Too Few Arguments" [cons requires an item and a list]
(cons "Jeff") returns "Error: Too Few Arguments" [cons requires an item and a list]
(cons 1 (list 2 3)) returns (1 2 3) [notice the difference here from the list function above]
(cons "Jeff" (list 1 2 3 5 6 7)) returns ( "Jeff" 1 2 3 5 6 7 )
nth - This function returns the Nth item in a list. The first item in a list is item zero.
Syntax : (nth integer list)
integer - Any valid integer.
list - Any valid list.
(nth) returns "Error: Too Few Arguments" [cons requires an item and a list]
(nth 2 1 2 3) returns "Error: Bad Argument Type" [nth requires an integer and a list]
(nth 2 (list "cde" "Jeff" "Sanders")) returns "Sanders"
(nth 0 (list 1 2 3)) returns 1
(nth 1 (list 1 2 3)) returns 2
(nth 2 (list 1 2 3)) returns 3
(nth 1 (list "a" (list "b" "c") "d")) returns ("b" "c")
(nth 4 (list 1 2 3 5 6 7)) returns 6
Example Program 1:
(defun C:myProg() [define program]
(setq pt1(getpoint "\n First Corner: ")) [get first point]
(setq pt2(getcorner pt1 "\n Last Corner: ")) [get other point on a square]
(setq x1(car pt1)) [get x coordinate of pt1]
(setq x2(car pt2)) [get x coordinate of pt2]
(setq y1(cadr pt1)) [get y coordinate of pt2]
(setq y2(cadr pt2)) [get y coordinate of pt2]
(setq hor1(- x2 x1)) [get horizontal distance]
(setq ver1(- y2 y1)) [get vertical distance]
(princ "\n Horizontal Distance = ")(princ hor1) [print to screen]
(princ "\n Vertical Distance = ")(princ ver1) [print to screen]
(princ) [supress echo..clean exit]
) [close program]
Execution:
Command: (load "myProg")<enter>
Command: myProg<enter>
Command: First Corner: <pick a point>
Command: Last Corner: <pick a point>
Command: Horizontal Distance = 23.5
Command: Vertical Distance = 11.5
Example Program 2:
(defun C:AddText() [define program]
(setq myList(list "e" "b" "c" "m" "at")) [make a list of 6 letters]
(foreach myVar myList [start the foreach loop]
(princ (strcat myVar (nth 4 myList))) [define a valid function]
) [close the foreach statement]
(princ) [supress echo..clean exit]
) [close program]
Execution:
Command: (load "AddText")<enter>
Command: AddText<enter>
Command: eat
Command: bat
Command: cat
Command: mat
Command: atat
End of List Functions