Sequence Iterating Functions

While in theory all operations on sequences boil down to some combination of **LENGTH**, **ELT**, and **SETF** of **ELT** operations, Common Lisp provides a large library of sequence functions.

One group of sequence functions allows you to express certain operations on sequences such as finding or filtering specific elements without writing explicit loops. Table 11-1 summarizes them.

Table 11-1.Basic Sequence Functions

NameRequired ArgumentsReturns
COUNTItem and sequenceNumber of times item appears in sequence
FINDItem and sequenceItem or NIL
POSITIONItem and sequenceIndex into sequence or NIL
REMOVEItem and sequenceSequence with instances of item removed
SUBSTITUTENew item, item, and sequenceSequence with instances of item replaced with new item

Here are some simple examples of how to use these functions:

  1. (count 1 #(1 2 1 2 3 1 2 3 4)) ==> 3
  2. (remove 1 #(1 2 1 2 3 1 2 3 4)) ==> #(2 2 3 2 3 4)
  3. (remove 1 '(1 2 1 2 3 1 2 3 4)) ==> (2 2 3 2 3 4)
  4. (remove #\a "foobarbaz") ==> "foobrbz"
  5. (substitute 10 1 #(1 2 1 2 3 1 2 3 4)) ==> #(10 2 10 2 3 10 2 3 4)
  6. (substitute 10 1 '(1 2 1 2 3 1 2 3 4)) ==> (10 2 10 2 3 10 2 3 4)
  7. (substitute #\x #\b "foobarbaz") ==> "fooxarxaz"
  8. (find 1 #(1 2 1 2 3 1 2 3 4)) ==> 1
  9. (find 10 #(1 2 1 2 3 1 2 3 4)) ==> NIL
  10. (position 1 #(1 2 1 2 3 1 2 3 4)) ==> 0

Note how **REMOVE** and **SUBSTITUTE** always return a sequence of the same type as their sequence argument.

You can modify the behavior of these five functions in a variety of ways using keyword arguments. For instance, these functions, by default, look for elements in the sequence that are the same object as the item argument. You can change this in two ways: First, you can use the :test keyword to pass a function that accepts two arguments and returns a boolean. If provided, it will be used to compare item to each element instead of the default object equality test, **EQL**.5 Second, with the :key keyword you can pass a one-argument function to be called on each element of the sequence to extract a key value, which will then be compared to the item in the place of the element itself. Note, however, that functions such as **FIND** that return elements of the sequence continue to return the actual element, not just the extracted key.

  1. (count "foo" #("foo" "bar" "baz") :test #'string=) ==> 1
  2. (find 'c #((a 10) (b 20) (c 30) (d 40)) :key #'first) ==> (C 30)

To limit the effects of these functions to a particular subsequence of the sequence argument, you can provide bounding indices with :start and :end arguments. Passing **NIL** for :end or omitting it is the same as specifying the length of the sequence.6

If a non-**NIL** :from-end argument is provided, then the elements of the sequence will be examined in reverse order. By itself :from-end can affect the results of only **FIND** and **POSITION**. For instance:

  1. (find 'a #((a 10) (b 20) (a 30) (b 40)) :key #'first) ==> (A 10)
  2. (find 'a #((a 10) (b 20) (a 30) (b 40)) :key #'first :from-end t) ==> (A 30)

However, the :from-end argument can affect **REMOVE** and **SUBSTITUTE** in conjunction with another keyword parameter, :count, that’s used to specify how many elements to remove or substitute. If you specify a :count lower than the number of matching elements, then it obviously matters which end you start from:

  1. (remove #\a "foobarbaz" :count 1) ==> "foobrbaz"
  2. (remove #\a "foobarbaz" :count 1 :from-end t) ==> "foobarbz"

And while :from-end can’t change the results of the **COUNT** function, it does affect the order the elements are passed to any :test and :key functions, which could possibly have side effects. For example:

  1. CL-USER> (defparameter *v* #((a 10) (b 20) (a 30) (b 40)))
  2. *V*
  3. CL-USER> (defun verbose-first (x) (format t "Looking at ~s~%" x) (first x))
  4. VERBOSE-FIRST
  5. CL-USER> (count 'a *v* :key #'verbose-first)
  6. Looking at (A 10)
  7. Looking at (B 20)
  8. Looking at (A 30)
  9. Looking at (B 40)
  10. 2
  11. CL-USER> (count 'a *v* :key #'verbose-first :from-end t)
  12. Looking at (B 40)
  13. Looking at (A 30)
  14. Looking at (B 20)
  15. Looking at (A 10)
  16. 2

Table 11-2 summarizes these arguments.

Table 11-2. Standard Sequence Function Keyword Arguments

ArgumentMeaningDefault
:testTwo-argument function used to compare item (or value extracted by :key function) to element.EQL
:keyOne-argument function to extract key value from actual sequence element. NIL means use element as is.NIL
:startStarting index (inclusive) of subsequence.0
:endEnding index (exclusive) of subsequence. NIL indicates end of sequence.NIL
:from-endIf true, the sequence will be traversed in reverse order, from end to start.NIL
:countNumber indicating the number of elements to remove or substitute or NIL to indicate all (REMOVE and SUBSTITUTE only).NIL