# Lab: Searching

Summary: In this laboratory, we explore different issues related to searching.

Contents:

## Exercises

### Exercise 0: Preparation

a. Start DrScheme.

b. If you have not done so already, please scan the reading on searching. In particular, you should look at the sample procedures. Make sure that you understand the purpose of get-key in binary-search.

c. Create a new file for this lab that contains the procedures from that reading.

d. Create a vector, names, of a dozen or so lists, each of which contains a last name and a first name. The first name and last name should both be strings. Order the list by last name. Include your name in the list. Include that vector in your file from step c.

### Exercise 1: Testing Our Procedures

a. Using sequential-search-list, search for the letter #\a in various lists of characters.

Note that it's probably easiest to create a list of characters with string->list. For example,

> (string->list "hello")
(#\h #\e #\l #\l #\o)

Note also that you will need to create your own predicate for this step of the exercise. I'd prefer that you'd create an anonymous procedure (one in which you write the lambda without a corresponding define).

b. Using sequential-search-vector, search for the letter #\a in various vectors of characters.

c. Develop some tests for search-list-for-keyed-value. For example, you might create a list of cartoon characters and their sidekicks and search the list for character or sidekick. Here is data for such a list.

Protagonist Sidekick
Peabody Sherman
Yogi Booboo
Secret Squirrel Morocco Mole
Tennessee Tuxedo Chumley
Quick Draw McGraw Baba Looey
Dick Dastardly Muttley
Bullwinkle Rocky
Bart Simpson Milhouse Van Houten
Asterix Obelix
Fred Barney

### Exercise 2: Extending sequential-search-vector

Write a procedure that takes a predicate and vector as parameters and, using sequential-search-vector as a helper, finds a value in the vector that matches the predicate or returns #f if no such value exists. (Like sequential-search-vector, this procedure searches vectors; unlike sequential-search-vector and like sequential-search-list, this procedure returns a matching value, rather than an index.

### Exercise 3: Observing Binary Search

Add calls to display and newline to the definition of binary-search, so that it prints out the values of lower-bound and upper-bound each time the kernel procedure is called. How many recursive calls are made as binary search finds your name in the list names (from the preparation)? How many are made in an unsuccessful search?

## For Those With Extra Time

If you find you finish the lab early, please attempt one or more of the following problems.

### Extra 1: Searching Files

Define and test a Scheme procedure, (search-file pred? port), that reads in Scheme values from a given input port, applying a specified test to each one. When it finds a value that passes the test, it should return that value; if it gets the end-of-file object before finding a value that passes the test, it should call the error procedure to print an appropriate diagnostic.

### Extra 2: A Guessing Game

The divide-and-conquer principle can be applied in other situations. For example, we can apply it to a guessing game in which one player, A, selects a number in the range from 1 to some value and the other player, B, tries to guess it by asking yes-or-no questions of the form Is your number less than n? (putting in specific values for n). The most efficient strategy for B to use is repeated bisection of the range within which A's number is known to lie.

Write a Scheme procedure that takes the part of B in this game. Your procedure should take the maximum possible value as a parameter. When invoked, it should print out a question of the specified form and read in the user's response (presumably, the symbol yes or the symbol no), then repeat the process until the range of possible values has been narrowed to contain only one number. The procedure should then display and identify that number. A sample run might look like this:

> (guessing-game 100)
Is your number less than 51? yes
Is your number less than 26? no
Is your number less than 38? no
Is your number less than 44? no
Is your number less than 47? yes
Is your number less than 45? no
Is your number less than 46? no
Since your number is less than 47 but not less than 46, it must be 46.

## History

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Tue May 6 09:29:17 2003.
The source to the document was last modified on Tue Apr 22 14:32:19 2003.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2003S/Labs/searching.html.

You may wish to validate this document's HTML ; ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu