Lab 4: Structures (onto Lists!)


(back to Lab 4 (part 1).)
;; A List-of-String is one of 
;; - empty
;; - (cons String List-of-String

;; Examples of List-of-string

(define lon-1 empty)
(define lon-2 (cons "Rose" empty))
(define lon-3 (cons "Carell" (cons "Foster" empty)))

cons is a convenient constructor for a structure with two fields. The second field in a cons must always be either empty or another value constructed with cons. In the Beginning Student language, we tend to call things constructed with cons "lists."

To pull out the first field in a cons, use the selector first To pull out the second field (which must always be empty or another cons), use the selector rest. Finally, to distinquish between the two variants of data, we use the predicates empty? and cons?.

;; Examples of using the selectors and predicates:
(equal? (empty? lon-1) true)
(equal? (empty? lon-2) false)
(equal? (first lon-2) "Rose")
(equal? (first (rest lon-3)) "Foster")

Exercise 13: What is the template for processing a List-of-string ?

Exercise 14: Design a function that, given a reservation book (aka a list of string) and a name, determines whether or not the name is actually in the reservation book. Make sure you try out your function on the stepper.

Exercise 15: Come up with a data definition that can represent the current occupancy of the restaurant. Remember, we need to represent a collection of tables, and the only thing we care about for each table is how many people are sitting at it.

Exercise 16: When a party leaves the restaurant, we need to update our occupancy structure to capture the fact that the party is not here any more. Design a function that consumes a occupancy structure and a number n (representing the number of people in the leaving party), and produces a new occupancy structure with only that party removed.

If the party is not on the occupancy structure (e.g., they snuck in to use the restroom), then just return an occupancy structure that is the same as the consumed occupancy structure.