Functional Problem Solving (CSC 151 2015F) : EBoards
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] - [FAQ] [Teaching & Learning] [Grading] [Taking Notes] [Rubric] [Remote Access]
Current: [Assignment] [EBoard] [Lab] [Outline] [Reading]
Sections: [Assignments] [EBoards] [Labs] [Outlines] [Readings] - [Examples] [Handouts]
Reference: [Setup] [VM] [Errors] - [Functions A-Z] [Functions By Topic] - [Racket] [Scheme Report (R5RS)] [R6RS] [TSPL4]
Related Courses: [Curtsinger (2015F)] [Davis (2013F)] [Rebelsky (2015S)] [Weinman (2014F)]
Misc: [Submit Questions] - [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] - [Issue Tracker (Course)]
Overview
Computer science is ... the study of algorithms and data
Computer scientists often try to write general algorithms.
Instead of darkest-color, we might write extreme (needs list
and also a way of comparing two elements of the list).
(define extreme (lambda (values compare) ...))
(extreme (list 87 1 2 6 3) >) ; finds the largest integer (extreme list-of-colors irgb-darker?) ; find the darkest color (extreme list-of-numbers (lambda (val1 val2) (< (abs val1) (abs val2)))) ; finds closest to zero
(define best (lambda (values better?) (let kernel ([guess (car values)] [remaining (cdr values)]) (cond [(null? remaining) guess] [(better? guess (car remaining)) (kernel guess (cdr remaining))] [else (kernel (car remaining) (cdr remaining))]))))
Computer scientists often look for class of problems.
Two common classes:
Once we've created an algorithm for solving a problem, we want to consider how many resources are required to run the algorithm. (Number of steps, amount of memory.)
Two versions of reverse on Monday's lab.
Whenever we find an algorithm, we consider its efficiency and then ask if we can do it better.
Look in the middle.
If the thing you are looking for is there, you are done!
Throw away the half that's irrelevant
Do it again.
Eventually you are left with one item (or none)
You are done.
;;; Procedure:
;;; binary-search
;;; Parameters:
;;; vec, a vector
;;; get-key, a function that extracts the important part (the "key"
;;; from an entry)
;;; may-precede?, a binary (two-parameter) predicate that let us
;;; decide which part to throw away
;;; (e.g., for numbers use < and for strings use string<=?
;;; key, the key we are looking for
;;; Purpose:
;;; Find the element with key key in vec.
;;; Produces:
;;; ???, a ???
(define binary-search
(lambda (vec get-key may-precede? key)
; Search a portion of the vector from lower-bound to upper-bound
(let search-portion ([lower-bound 0]
[upper-bound (- (vector-length vec) 1)])
; If the portion is empty
(if (> lower-bound upper-bound)
; Indicate the value cannot be found
-1
; Otherwise, identify the middle point, the element at that
; point and the key of that element.
(let* ([midpoint (quotient (+ lower-bound upper-bound) 2)]
[middle-element (vector-ref vec midpoint)]
[middle-key (get-key middle-element)]
[left? (may-precede? key middle-key)]
[right? (may-precede? middle-key key)])
(cond
; If the middle key equals the value, we use the middle value.
[(and left? right?)
midpoint]
; If the middle key is too large, look in the left half
; of the region.
[left?
(search-portion lower-bound (- midpoint 1))]
; Otherwise, the middle key must be too small, so look
; in the right half of the region.
[else
(search-portion (+ midpoint 1) upper-bound)]))))))