20-CS-4003-001 Organization of Programming Languages Fall 2017

Lambda calculus, Type theory, Formal semantics, Program analysis

    Prev     Next     All lectures        Code

Procedures: Binary Search

(define bs
  (lambda (x lst low high)
    (let ((mid (floor (/ (+ high low) 2))))
          (if (< high low)
              (if (= x (list-ref lst mid))
                  (if (< x (list-ref lst mid))
                      (bs x lst low (- mid 1))
                      (bs x lst (+ mid 1) high)))))))

(define search
  (lambda (x lst)
     (bs x lst 0 (- (length lst) 1))))
 -  Classic binary search program. Given a list lst of integers in increasing order, return the position of x in the list or -1 if x is not present.