; vec-search-down? : (any -> boolean) (vectorof any) -> boolean ; Purpose: Returns whether any element of the vector meets the given ; predicate. ; Examples: ; (vec-search-down? number? (vector true false)) = false ; (vec-search-down? number? (vector true 3 false)) = true ; (vec-search-down? number? (vector)) = false (define (vec-search-down1? pred? a-vec) (local [;; search : natnum -> boolean ;; Purpose: Returns whether any element 0..num_elts-1 of a-vec ;; meets predicate pred?. (define (search num_elts) (cond [(zero? num_elts) false] [else (or (pred? (vector-ref a-vec (sub1 num_elts))) (search (sub1 num_elts)))]))] (search (vector-length a-vec)))) (define (vec-search-down2? pred? a-vec) (local [;; search : natnum -> boolean ;; Purpose: Returns whether any element 0..index of a-vec ;; meets predicate pred?. (define (search index) (cond [(zero? index) (pred? (vector-ref a-vec index))] [else (or (pred? (vector-ref a-vec index)) (search (sub1 index)))]))] (search (sub1 (vector-length a-vec))))) ; Note: vec-search-down2? does NOT work with zero-length vectors, ; i.e., (vector). To make it work, you need to change the base case, ; e.g., to ; [(= -1 index) false] ; or ; [(>= 0 index) false] ; That would result in essentially the first version, except that ; the meaning of "index" and "num_elts" would differ by 1. ; Since that base case is slightly more complicated than that of ; vec-search-down1?, we slightly prefer vec-search-down1?.