List Manipulation


In this article, we will discuss how to process the List in Lisp. As you may be well aware, LISP stands for “List Processing”.

A list is a group of elements consisting of any data type and is stored as a single variable. A list can contain any number of Reals, Integers, Strings, Variables and even other Lists. Let’s have a look at a list. Type this :

(setq pt1 (getpoint “\nChoose a Point : “))

AutoLisp should return something like this : (127.34 35.23 0.0)

Now what do I do with the list? AutoLisp has many functions available to manipulate lists. Let’s take a look at them.

Car

The primary command for taking a list apart is the “Car” function. This function returns the first element of a list. (The x coordinate.)

For example :

(setq a (car pt1))

Would return : (127.34)

Cdr

This function returns the second element plus the remaining elements of a list.

For example :

(setq b (cdr pt1))

Would return : (35.23 0.0)

But what if we only wanted the second element? We could write :

(setq b (car (cdr pt1)))

There is a better way to do this task. AutoLisp provides the “Cadr” function which is basically an abbreviation of a nested command.

Cadr

This returns the second element of a list. (The y coordinate.)

(setq b (cadr pt1))

This would return : (35.23)

Likewise, there is another abbreviated function to return the third element.

Caddr

This returns the third element of a list. (The z coordinate.)

(setq c (caddr pt1))

Would return: (0.0)

AutoLisp has other functions that will retrieve values from lists of more than three elements. (Caar, cadar, etc). You can also use the nth function to access any element of a list.

nth

The syntax for the nth function is as follows : (nth num list)

“num” is the number of the element to return. Just remember that zero is the first element. For example given the list :

(setq d ‘(“M10” “M20” “M30” 10.25))
(setq e (nth 0 d))

Would return : (“M10”)

And likewise : (setq f (nth 3 d))

Would return : (10.25)

We have now managed to extract elements from a list, but what do you do if you want to create a new list ? Let’s say you have two elements :


(setq a 200.0)
(setq b 400.0)

You want to combine them to create a new list. To do this you would use the “List” function. For example :

(setq c (list a b))

Would return : (200.0 400.0)

You could also write the function like this: (setq c ‘(a b))

Here is an example of List Manipulation. We are going to use the (car), (cadr) and (list) function to draw a rectangle.

(defun c:rec ( / pt1 pt2 pt3 pt4)
(setq pt1 (getpoint “\nSelect First Corner: “))
;get the first point
(setq pt3 (getcorner pt1 “\nSelect Second Corner: “))
;get the third point
(setq pt2 (list (car pt1) (cadr pt3)))
;construct the second point
(setq pt4 (list (car pt3) (cadr pt1)))
;construct the fourth point
(command “Line” pt1 pt2 pt3 pt4 “c”)
;draw the rectangle
(princ)
);defun

Let’s look closer at the line :

(setq pt2 (list (car pt1) (cadr pt3)))

This function retrieves the first element (x coordinate) from the list pt1, the second element (y coordinate) from the list pt3, creates a list from these elements and stores the list in the variable pt2.

The following diagram should help you to better understand this.

CADopia LISP

AutoLisp provides other functions to manipulate lists. Let’s take a look at some of them.

Last

Will return the last element of a list :

(last ‘(“M20” “M24” “M30”))

Would return : (“M30”)

Length

This returns an integer indicating the number of elements in a list :

(length ‘(“M20” “M24” “M30”))

Should return :(3)

Member

This function searches a list for a specific element. If found it returns the element plus the remainder of the list :

(member ‘c ‘(a b c d e f))

Would return :(c d e f)

Reverse

Returns a list with it’s elements reversed :

(reverse ‘(a b c d e f))


Will Return :(f e d c b a)

Subst

Searches a list for an old element and returns a copy of the list with the new item substituted in place of every occurrence of the old item :

Syntax: (subst newitem olditem lst)

(subst ‘f ‘c lst)

Would return (a b f d e f)