7.5.30.5 SRFI-43 Searching

Scheme Procedure: vector-index pred? vec1 vec2 …

Find and return the index of the first elements in vec1 vec2 … that satisfy pred?. If no matching element is found by the end of the shortest vector, return #f.

(vector-index even? '#(3 1 4 1 5 9))
⇒ 2
(vector-index < '#(3 1 4 1 5 9 2 5 6) '#(2 7 1 8 2))
⇒ 1
(vector-index = '#(3 1 4 1 5 9 2 5 6) '#(2 7 1 8 2))
⇒ #f
Scheme Procedure: vector-index-right pred? vec1 vec2 …

Like vector-index, but it searches right-to-left, rather than left-to-right. Note that the SRFI 43 specification requires that all the vectors must have the same length, but both the SRFI 43 reference implementation and Guile’s implementation allow vectors with unequal lengths, and start searching from the last index of the shortest vector.

Scheme Procedure: vector-skip pred? vec1 vec2 …

Find and return the index of the first elements in vec1 vec2 … that do not satisfy pred?. If no matching element is found by the end of the shortest vector, return #f. Equivalent to vector-index but with the predicate inverted.

(vector-skip number? '#(1 2 a b 3 4 c d)) ⇒ 2
Scheme Procedure: vector-skip-right pred? vec1 vec2 …

Like vector-skip, but it searches for a non-matching element right-to-left, rather than left-to-right. Note that the SRFI 43 specification requires that all the vectors must have the same length, but both the SRFI 43 reference implementation and Guile’s implementation allow vectors with unequal lengths, and start searching from the last index of the shortest vector.

Scheme Procedure: vector-binary-search vec value cmp [start [end]]

Find and return an index of vec between start and end whose value is value using a binary search. If no matching element is found, return #f. The default start is 0 and the default end is the length of vec.

cmp must be a procedure of two arguments such that (cmp a b) returns a negative integer if a < b, a positive integer if a > b, or zero if a = b. The elements of vec must be sorted in non-decreasing order according to cmp.

Note that SRFI 43 does not document the start and end arguments, but both its reference implementation and Guile’s implementation support them.

(define (char-cmp c1 c2)
  (cond ((char<? c1 c2) -1)
        ((char>? c1 c2) 1)
        (else 0)))

(vector-binary-search '#(#\a #\b #\c #\d #\e #\f #\g #\h)
                      #\g
                      char-cmp)
⇒ 6
Scheme Procedure: vector-any pred? vec1 vec2 …

Find the first parallel set of elements from vec1 vec2 … for which pred? returns a true value. If such a parallel set of elements exists, vector-any returns the value that pred? returned for that set of elements. The iteration is strictly left-to-right.

Scheme Procedure: vector-every pred? vec1 vec2 …

If, for every index i between 0 and the length of the shortest vector argument, the set of elements (vector-ref vec1 i) (vector-ref vec2 i) … satisfies pred?, vector-every returns the value that pred? returned for the last set of elements, at the last index of the shortest vector. Otherwise it returns #f. The iteration is strictly left-to-right.