Home. Page II. Page III.

### List Manipulation - Page I.

#### As you are probably well aware, LISP stands for "List Processing". (Not "Lost in Stupid Parenthesis") 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)
```

Fine, you say, I've got a list but what do I do with it?
AutoLisp has many functions available to manipulate lists.
Let's have 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)))
```

But there is a better way. AutoLisp has provided the "Cadr" function which is basically an abbreviation of a nested command.

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.

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, though, us another function to access any element of a list. This is the "nth" function.

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)
```

On the next page we will look at a practical example of using these functions.

 Home. Page II. Page III.