f="style.css" title="style1"> cs150: Problem Set 5: Wahoo! Auctions
cs150  Spring 2009

cs150: Computer Science
from Ada and Euclid to Quantum Computing and the World Wide Web


Instructor
Westley Weimer

Teaching Assistants
Zak Fry
Paul DiOrio
Rachel Lathbury

Email Address
cs150-staff@cs.virginia.edu

Class Meetings
Mondays and Wednesdays, 3:30-4:45pm in MEC 341
Structured Lab Hours
Wednesdays, 7:00-8:00pm and 8:00-9:00pm in OLS 001
Staffed Lab Hours
(Small Hall Lab)

Monday 5:00-6:00 (Zak)
Tuesday 3:15-4:15 (Rachel)
Thursday 5:00-6:00 (Paul)
Sunday 3:00-4:00 (on request)
Office & Lab Hours
(Small Hall Lab)

Monday 2:00-3:00 (Rachel)
Tuesday 11:00-12:00 (Wes in Olsson 219)
Tuesday 3:00-4:00 (Zak)
Wednesday 1:00-2:00 (Paul)

="ps5.zip">ps5.zip to your machine and unzip it into your home directory J:\cs150\ps5.

This file contains:

Background

Once upon a time, in a faraway fictional town known as Charlottansville, East Virginia, there was a highly regarded public university where funding cuts led to a budget crisis. The wise and noble University administration tried cutting "non-essential" services such as heat and cleaning for student housing, libraries, printing and professors and have attempted some more creative solutions including hosting rock concerts, building a fancy new basketball arena, increasing student tuition, installing fancy electronic scoreboards on the intramural sports fields, and increasing student housing fees, but somehow the budget is still not balanced and the University is anticipating many millions of dollars in future firing expenses.

Fortunately, the University was able to get "Chapter 11" status from the Commonwealth, which gives it more power to control its own destiny by raising tuition and selling off assets. The Board of Transients has decided the time for drastic measures has arrived, and they will conduct an auction to raise money. To avoid paying fees to a commercial auction service, they have decided to exploit free labor from cs150 students to develop an auction service for them.

The auction service will be built using a database. A database is just a way of storing and manipulating a large amount of structured data. For this problem set, you will implement a database and use it to implement an auction service. Our database is controlled using Scheme procedures that are similar to the SQL commands used to control nearly all commercial databases. In the final course project, you will build a dynamic web site that uses SQL commands to interact with a database.

Data in a database is stored in tables. The fields of a table describe the data and are not mutable. The entries of a table are the data, and can be changed. We will represent a table using a cons pair where the car is a list of the table fields and the cdr is a list of the table entries. These definitions are found in database.scm:

     (define (make-new-table fieldlist) (cons fieldlist null))
     (define (make-table fieldlist entries) (cons fieldlist entries))
     (define (table-fields table) (car table))
     (define (table-entries table) (cdr table))
For the Wahoo! Auctions service, we will need several different tables to keep track of the bidders, the items for sale, and the bids on those items. We use quoted symbols to describe the fields in a table. Those tables are defined in auction.scm:
     (define bidders (make-new-table (list 'name 'email)))
     (define items (make-new-table (list 'item-name 'description)))
     (define bids (make-new-table (list 'bidder-name 'item-name 'amount)))
Question 1: Draw the global environment after (define bidders (make-new-table (list 'name 'email))) is evaluated. You only need to show the environment relevant to the value of bidders.

Hint: See the lecture slides from Class 13 or Figure 10.1 from the Course Book for examples of drawn global environments.

Question 2:
a. Draw the global environment after the following expressions are evaluated (starting in a clean global environment where make-new-table, table-fields and table-entries are defined as above):
   (define t1 (make-new-table (list 'name 'email)))
   (set-car! (table-fields t1) 'nom)
   (set-cdr! t1 t1)
b. Suppose we then evaluate:
   (define (length lst) (if (null? lst) 0 (+ 1 (length (cdr lst)))))
   (length t1)
Explain why the evaluation of (length t1) never terminates.

Try evaluating the expressions in Question 2 in DrScheme. Also evaluate t1 and see if you can figure out what the printed value means.

Inserting Entries

In Question 2, we manipulated our tables using set-car! and set-cdr!. It is awkward and error-prone to manipulate our database that way. Instead, we want to define more abstract procedures for manipulating data tables.

The table-insert! procedure inserts an entry into a table. We follow the Scheme (and Yahoo!?) convention of using an ! at the end of the names of procedures that mutate state. You should follow this convention also.

You shouldn't need to change the definition of table-insert!, but you should be able to understand it (defined in database.scm):

(define (table-insert! table entry)
  (assert (= (length entry) (length (table-fields table))))
  (if (null? (table-entries table))
      (set-cdr! table (list entry))
      (append! (table-entries table) (list entry)))
  (void)) ;;; don't evaluate to a value
The expression (assert (= (length entry) (length (table-fields table)))) checks that the entry we are adding to the table has the right number of elements — there must be one element in the entry corresponding to each field in the table. The assert procedure will produce an error if the passed parameter is false. It is defined:
(define (assert pred)
  (if (not pred) (error "Assertion failed!")))
We use (void) at the end of the procedure body to prevent an application of table-insert! from evaluating to a value. The procedure void takes no parameters, and produces no value.

Question 3:
a. Why does the definition of table-insert! use append! instead of append? (Your answer should clearly explain what would go wrong if we used append.)

b. Why does the definition of table-insert! need to do something different in the case where (table-entries table) is null?

Hint: try evaluating

    (define lst null) 
    (append! lst 3) 
    lst

Our auction service will need procedures for adding bidders, posting items for sale and bidding on items.

Question 4:
a. We have already provided a definition of add-bidder! in ps5.scm. It takes two parameters (name and email) and adds an entry in the bidders table. Understanding it should give you a good idea how to define some additional procedures. Define post-item! and insert-bid!.

Question 5: Describe the running time of your insert-bid! procedure. Your answer should use Θ notation, clearly explain what all variables you use mean, and include an explanation of why your answer is correct.

We have provided a table-display procedure in database.scm for printing out a table. (You don't need to understand the details of how table-display works.)

If you define add-bidder!, post-item! and insert-bid! correctly, you should obtain the following interactions:

> (table-display bidders)

name                       email                      
-------------------------  -------------------------  

> (add-bidder! "Tim Koogle" "tk@yahoo.com")

> (add-bidder! "Katie Couric" "katie@cbs.com")

> (add-bidder! "Dave Matthews" "dave@dmb.com")

> (table-display bidders)

name                       email                     
-------------------------  ------------------------- 
Tim Koogle                 tk@yahoo.com              
Katie Couric               katie@cbs.com
Dave Matthews              dave@dmb.com              
Develop similar tests for your post-item! and insert-bid! procedures, and make sure they also work correctly.

Selecting Entries

Inserting entries in tables is only useful if we can also get the entries we are interested in out of the table. The table-select procedure takes a table, a field and a procedure. It evaluates to a table that has the same fields as the input table, and has entries that are all entries in the table for with the procedure applied to the value of the field for that entry is true. For example, in auction.scm we define:
  (define (make-string-selector match) (lambda (fval) (string=? fval match)))

  (define (get-bids item)
    (table-entries (table-select bids 'item-name (make-string-selector item))))
The get-bids procedure evaluates to a list of bid entries that are the entries in the bids table whose item-name field matches the parameter item. If you're not sure how this works, try evaluating some expressions using make-string-selector by themselves.

Question 6: Define the table-select procedure. You may find the find-element-number, get-nth and list-filter procedures defined in listprocs.scm useful (but are not required to use them).

If your table-select is working correctly, you should see interactions similar to these:

(Remember our University is completely fictional. The bid amounts should not be construed to have anything to do with the actual value of any aspects of any real Universities with which you might be familiar (or with the generosity of actual University alumni who might have similar names.)

(insert-bid! "Tim Koogle" "SEAS" 10000000)

(insert-bid! "Dave Matthews" "CLAS" 2000000)

(insert-bid! "Tiki Barber" "COMM" 79000000)

(insert-bid! "Katie Couric" "CLAS" 37000000)

(table-select bids 'item-name (lambda (pitem) (string=? pitem "CLAS")))

((bidder-name item-name amount) ("Dave Matthews" "CLAS" 2000000) ("Katie Couric" "CLAS" 37000000))

(table-display (table-select bids 'item-name (lambda (pitem) (string=? pitem "CLAS"))))

bidder-name                item-name                  amount
-------------------------  -------------------------  -------------------------
Dave Matthews              CLAS                       2000000
Katie Couric               CLAS                       37000000

(table-entries (table-select bids 'item-name (lambda (pitem) (string=? pitem "SEAS"))))

(("Tim Koogle" "SEAS" 10000000))

(table-entries (table-select bids 'item-name (lambda (pitem) (string=? pitem "Rotunda"))))

()

(table-entries (table-select bids 'amount (lambda (pamount) (> pamount 10000000))))

(("Tiki Barber" "COMM" 79000000) ("Katie Couric" "CLAS" 37000000)

(table-entries
    (table-select
      (table-select bids 'amount (lambda (pamount) (<= pamount 10000000)))
      'bidder-name
      (lambda (pbidder) (string=? pbidder "Katie Couric"))))

()

Selects can be nested. Here we use (table-select bids 'amount (lambda (pamount) (<= pamount 10000000))) to produce the table of all bids over 10000000, and then use another table-select to select the bids from that table where the bidder name is "Katie Couric". To make sure you understand table-select, produce a different expression that will evaluate to the same table by selecting the name first and then the amount.
Question 7: Describe the amount of work in an efficient table-select procedure is using Θ notation. Be careful to specify carefully what any variables you use in your answer mean.

Highest Bids

In any auction, it is important to know who currently has the highest bid.
Question 8: Define a get-highest-bid procedure that takes an item name as a parameter and evaluates to the bid entry that is the highest bid on that item. If there is no bid, return null. You shouldn't assume that the last entry that is a bid on this item is the highest bid for that item.

Your get-highest-bid procedure should work like this:

> (setup-tables)

> (get-highest-bid "SEAS")

("Tim Koogle" "SEAS" 10000000)

> (get-highest-bid "Rotunda")

()

Placing Bids

To conduct fair auctions, we can't just allow any bid to be inserted into the bids table. Instead, bids will be placed using a place-bid procedure. It should:

To report an error, use the (error "message") function. For example: (error bidder "is not an authorized bidder!"). For the purposes of grading, the exact prose of your error message doesn't matter — only whether or not you indicate an error. So be as creative as you like with your error messages.

Question 9: Define a new place-bid procedure that satisfies the description above. Don't attempt to do everything at once! Start by satisfying one of the properties first and testing your procedure before trying to satisfy the other property.

Hint: place-bid may be a relatively long procedure: 25 to 30 lines is reasonable. Don't worry if yours is longer or shorter, as long as you understand what is going on and you are getting the right answers.

You should get interactions similar to these:

(setup-tables)

(place-bid "Tim Koogle" "SEAS" 20000000)

#t

(place-bid "Katie Couric" "SEAS" 18000000)

Bid amount does not exceed previous highest bid: {"Tim Koogle" "SEAS" 20000000}

(place-bid "Katie Couric" "SEAS" 22000000)

#t

(place-bid "Dave Matthews" "The Rotunda" 1000000)

The Rotunda "is not for sale!"

(place-bid "Westley Weimer" "SEAS" 10000000000)

Westley Weimer "is not a legitimate bidder!"

Ending the Auction

When the auction is over, all items are sold to the highest bidders and the University collects the high bid amounts. If there are no bids for an item, it cannot be sold.

Question 10: Define a function end-auction that reports the final auction results. You may assume that the auction has been conducted fairly (i.e., using place-bid and not by randomly mutating the tables). Your function should return a table with three fields: bidder-name, item-name and amount. There should be one entry in the table for each winning bid (and thus at most one entry per item). Losing bids and items with no bids should not appear.

Here's an example:

(setup-tables)

(place-bid "Tim Koogle" "SEAS" 18000000)

#t

(place-bid "Katie Couric" "CLAS" 90000000)

#t

(table-display (end-auction))

bidder-name       item-name      amount
----------------  -------------  ---------
Tiki Barber       COMM           79000000 
Tim Koogle        SEAS           18000000 
Katie Couric      CLAS           90000000 

Try some sample auctions to demonstrate your program. See if you can make enough money to save the University, but be careful not to sell off too many important assets — we will revisit the University in Charlottansville in Problem Set 6.

Question 11: Describe the running time of your end-auction procedure. (You answer should use Θ notation, should clearly define the meaning of all variables you use in your answer, and should include a convincing explanation of why it is correct.)

Question 12: Define a variable book-comments that is a string containing your thoughts on the Course Book (that's the one available online by David Evans, not GEB) so far. Your string may contain line breaks if you like, or it can be one long line. Example:

(define book-comments "You, book, are ugly. But in the morning, I shall be sober. A weakness of this book is that knowledge attained by rote memorization is tantamount to a defeat, for it is momentary. A strength of this book is that if you know this textbook and know yourself, you will not be imperiled in a hundred battles. Finally, I note a mistake on page 1937, where you suggest that it is peace in our time -- clearly a typo.")

More seriously, we covet comments on what you thought was well-explained in the book and what was confusing, as well as what you might have done differently. If you notice any errors in the text, please bring them to our attention. Your comments will reach the textbook author directly, and help me to make the course better.

If you are working with a partner, make a longer comment that addresses all of your concerns.

In any event, you will receive full credit for any comment longer than 20 characters.

Question 13: Define a variable worst-section that is a cons pair of two strings. The first string should be the label of one of the sections in the course book, and the second string should explain why you didn't like that section. For example,

(define worst-section (cons "1.2.2" "The van Gogh pictures make me dizzy."))

Your comments should be more substantive and helpful than that example. In any event, you will receive full credit for any comment longer than 20 characters. Students who submit exceptionally helpful book-comments and worst-section answers will receive extra credit.

Question 14: Define a variable favorite-fractals that is a list of three integers. Each integer should correspond to a Fractal from the CS 150 Fractal Gallery. You may use any evaluation criteria you like. You may vote for a fractal multiple times. If you are working with a partner, you and your partner may (should?) vote for different fractals — since each partner submits a separate copy of the assignment, each partner gets three separate votes. You are on your honor not to vote for your own factal. The order of your list does not matter.

The creators of the fractals that get the most votes will receive extra credit and public acclaim.

Automatic Adjudication: Submit a single Scheme Definition file that addresses Questions 4, 6, 8, 9, 10, 12 and 13 until you are satisfied with your test results. Your scheme file should be a modification of the ps5.scm file. Each partner must submit the file separately.

Credits: This problem set was created for CS200 Spring 2003 by Katie Winstanley, Rachel Dada, Spencer Stockdale and David Evans, revised for CS200 Spring 2004 by Sarah Bergkuist and David Evans, revised for CS150 Fall 2005 by David Evans, and finally revised for CS150 Spring 2009 by Wes Weimer. Any similarity between the events depicted in this problem set and the goings on at a real University are purely coincidental, especially the parts about needing to pay millions of dollars to replace a basketball coach, selling assets to the highest bidders regardless how many people they kill, and the Board of Visitors deciding to spend thousands of dollars to hide empty seats in the new basketball arena at the same meeting where they discussed increasing student housing rates because the first year dorms are deteriorating.
[an error occurred while processing this directive] cs150: Problem Set 5: Wahoo! Auctions
cs150  Spring 2009

cs150: Computer Science
from Ada and Euclid to Quantum Computing and the World Wide Web


Instructor
Westley Weimer

Teaching Assistants
Zak Fry
Paul DiOrio
Rachel Lathbury

Email Address
cs150-staff@cs.virginia.edu

Class Meetings
Mondays and Wednesdays, 3:30-4:45pm in MEC 341
Structured Lab Hours
Wednesdays, 7:00-8:00pm and 8:00-9:00pm in OLS 001
Staffed Lab Hours
(Small Hall Lab)

Monday 5:00-6:00 (Zak)
Tuesday 3:15-4:15 (Rachel)
Thursday 5:00-6:00 (Paul)
Sunday 3:00-4:00 (on request)
Office & Lab Hours
(Small Hall Lab)

Monday 2:00-3:00 (Rachel)
Tuesday 11:00-12:00 (Wes in Olsson 219)
Tuesday 3:00-4:00 (Zak)
Wednesday 1:00-2:00 (Paul)