View Single Post
Old 12-17-2002, 08:38 PM   #5
feanor
 
Posts: n/a
Some fine scheme code:

; Problem #1, vector-linear-search
;
; vector-linear-search takes a vector (c) and a predicate (pred). It then
; searches through the successive components of v starting from the left end
; and returns the index of the leftmost component of v satisfying pred. If
; no such component is found, it returns -1.
;
(define vector-linear-search
(lambda (v pred)
(let ((size (vector-length v)))
(letrec ((helper
(lambda (i)
(cond
((= i (sub1 size))
(begin
(if (apply pred (list (vector-ref v i)))
i
-1)))
((apply pred (list (vector-ref v i)))
i)
(else
(helper (add1 i)))))))
(cond
((zero? size) -1)
(else
(helper 0)))))))

Ahh

Bryan
  Reply With Quote