next up previous contents index
Next: Combining Several Lists Up: More on Generating Previous: Generating Systematic Data

Forming Subsets and Deleting Cases

  The select   function allows you to select a single element or a group of elements from a list or vector. For example, if we define x by

(def x (list 3 7 5 9 12 3 14 2))

then (select x i) will return the i -th element of x . Lisp, like the language C but in contrast to FORTRAN, numbers elements of list and vectors starting at zero. Thus the indices for the elements of x are 0, 1, 2, 3, 4, 5, 6, 7   . So
> (select x 0)
3
> (select x 2)
5

To get a group of elements at once we can use a list of indices instead of a single index:
> (select x (list 0 2))
(3 5)

If you want to select all elements of x except element 2 you can use the expression

(remove 2 (iseq 0 7))

  as the second argument to the function select :
> (remove 2 (iseq 0 7))
(0 1 3 4 5 6 7)
> (select x (remove 2 (iseq 0 7)))
(3 7 9 12 3 14 2)

Another approach is to use the logical function /=   (meaning not equal) to tell you which indices are not equal to 2. The function which   can then be used to return a list of all the indices for which the elements of its argument are not NIL :
> (/= 2 (iseq 0 7))
(T T NIL T T T T T)
> (which (/= 2 (iseq 0 7)))
(0 1 3 4 5 6 7)
> (select x (which (/= 2 (iseq 0 7))))
(3 7 9 12 3 14 2)

This approach is a little more cumbersome for deleting a single element, but it is more general. The expression (select x (which (< 3 x))) , for example, returns all elements in x that are greater than 3:
> (select x (which (< 3 x)))
(7 5 9 12 14)



next up previous contents index
Next: Combining Several Lists Up: More on Generating Previous: Generating Systematic Data



Anthony Rossini
Fri Oct 20 10:29:17 EDT 1995