Functional Problem Solving (CSC 151 2015S) : EBoards

CSC151.01 2015S, Class 46: Binary Search Lab


Overview

Preliminaries

Admin

Upcoming Work

Extra Credit Opportunities

Academic

Peer Support (Morning Section)

Miscellaneous

Other Good Things (no extra credit)

Questions

Exploring the search API

;;; Procedure:
;;;   binary-search
;;; Parameters:
;;;   vec, a vector of something
;;;   key, a value
;;;   get-key, a unary procedure
;;;   may-precede?, a binary predicate (or comes-before?)
;;; Purpose:
;;; Produces:
;;; Preconditions:
;;;   vec is ordered by key.  That is, for all reasonable i,
;;;     (may-precede? (get-key (vector-ref vec i)) 
;;;                   (get-key (vector-ref vec (+ i 1))))
;;; Postconditions:

A few quick examples

; Each event has a name, a location, and an expected attendance
(define events-on-campus
  (vector 
    (list "CS Table" "Day PDF" 10)
    (list "Convo: Personal Memory" "JRC 101" 150)
    (list "Titular head" "Harris Cinema" 1800)))

If ordered by capacity

 (binary-search events-on-campus 150 (o car cdr cdr) <=)

If ordered by title

 (binary-search events-on-campus "Titular Head" car string-ci<=?)

If we use vectors rather than lists, ordered by capacity ; Each event has a name, a location, and an expected attendance (define events-on-campus (vector (vector "CS Table" "Day PDF" 10) (vector "Convo: Personal Memory" "JRC 101" 150) (vector "Titular head" "Harris Cinema" 1800)))

(binary-search events-on-campus 211 (r-s vector-ref 2) <=)
(binary-search events-on-campus 211 (lambda (event) (vector-ref event 2)) <=)

Lab