On this page:
The Three Cases
Databases
Before You Go...
Stretch Goals
6.6

Lab 8 Multiple Complex Inputs

lab!

Purpose: The purpose of this lab is to practice processing multiple complex inputs.

Textbook References: Chapter 23: Simultaneous Processing

The Three Cases

Exercise 1 Design the function alternate which, given two lists produces a list of alternating elements from each list. For example, (alternate (list 1 2) (list 3 4 5)) should produce (list 1 3 2 4 5). If one list runs out of elements you should place all the remaining elements from the other list at the end (so (alternate (list 1 2 3) empty) will produce (list 1 2 3)).

Exercise 2 Design the function cross-product which, given two lists produces a cross product of their elements. For example, given the list (list "a" "b") and the list (list 1 2 3), the function should produce (list (list "a" 1) (list "a" 2) (list "a" 3) (list "b" 1) (list "b" 2) (list "b" 3)).

Exercise 3 Design the function map.v2 which takes in two lists of the same length and performs an operation on both of them. So, for example (map.v2 (list 1 2) (list 3 4) *) would produce (list (* 1 3) (* 2 4)).

Databases

Consider the following definitions for a database of information:

; An Atom is one of:
; - String
; - Number
; - Boolean
 
(define-struct column [label pred])
; A Column is a (make-column String [Atom -> Boolean])
; - where label is the name of the column
; - and pred is the function that determines if a value fits in this column
 
; A Schema is a [List-of Column]
; and represents the names and requirements for each column in a database
 
; A ContentRow is a [List-of Atom]
; and represents a row of content in a database
 
; A ContentGrid is a [List-of ContentRow]
; and represents a list of the rows in a database
 
(define-struct db [schema content])
; A Database is a (make-db Schema ContentGrid)
; - where schema is a list of the labels and predicates for each column
; - and content is a grid of the content cells in the database

As you can see there are two aspects to a database: the columns and the rows. However, these two aspects are linked: each row in the database must have all the columns described by the Schema and each entry in the row must pass the test for the corresponding column. To help explain these definitions, we have provided some examples for you:

(define COLUMN1 (make-column "Name" string?))
(define COLUMN2 (make-column "Age" number?))
(define COLUMN3 (make-column "Graduated?" boolean?))
 
(define ROW1 (list "Becca" 26 true))
(define ROW2 (list "Kyle" 20 false))
 
(define DATABASE1 (make-db (list COLUMN1 COLUMN2 COLUMN3) (list ROW1 ROW2)))

As you can see, this database has 3 columns. There is a column for name, which must be a String; a column for age, which must be a Number; and a column for whether the person has graduated, which must be a Boolean. Each row in the database contains all 3 of these columns and each entry passes the predicate for the corresponding column.

Exercise 4 Define the templates for each of the provided data definitions, and provide another example of a database with different columns.

Exercise 5 To get started, design the function valid-database? which checks that each row in a database’s content has the correct number of columns and that each entry passes the predicate for its column. Therefore (valid-database? DATABASE1) should return true.

Exercise 6 One thing we want to be able to do with databases is display them. Design the function view-database that takes a Database and produces an image of that database. The column headers should be displayed in a larger font than the database entries, to differentiate between them.

One thing we want to be able to do is edit our database, but in order to test functions that edit our database we will need to be able to compare two databases.

Exercise 7 Stop and consider why we cannot easily compare two databases using check-expect. If you aren’t sure, ask a staff member before continuing.

Exercise 8 Design the function same-database? which takes two Databases and determines if they have the same column header names, and the same data in those columns. For the purposes of this problem you may ignore the column predicates.

Exercise 9 Design the function db-union which takes two Databases with the same Schemas and produces a database with that schema and all the data from both databases. Your function should eliminate duplicate rows. You may assume that the schema for both databases is the same.

Before You Go...

If you had trouble finishing any of the exercises in the lab or homework, or just feel like you’re struggling with any of the class material, please feel free to come to office hours and talk to a TA or tutor for additional assistance.

Stretch Goals

Exercise 10 Design the function db-intersect which takes two Databases and produces a single database which has only the columns in both the databases. You may choose the predicate from either database as long as the column names match. The data in that column should be the data from both databases in that column (eliminating duplicate rows, of course).

Exercise 11 A database is really the same as another database if it contains the same data in any order. Update your same-database? function so that two databases are the same even if their columns are in a different order or their rows are in a different order.