Comp 210 Lab 3: More Data Structures

Index: List and structure examples, HTML library

There are too many examples here to do all during lab. Instead, do some from each group during lab, and the rest on your own. We will continue with these and similar examples in the next lab, so that you get lots of practice.

Important for all examples:


List and structure examples

In class, we've defined, e.g.,

     ; a list-of-symbols is one of
     ;   - empty
     ;   - (make-lst f r), where f is a symbol, and r is a list-of-symbols
     (define-struct lst (first rest))
and then used make-lst, lst-first, lst-rest, and lst?. Using lists is so common that Scheme has these functions built in, except using different names. Instead, write
     ; a list-of-symbols is one of
     ;   - empty
     ;   - (cons f r), where f is a symbol, and r is a list-of-symbols
and use cons, first, rest, and cons?.

Lists of numbers

To do:

  1. Make the data definition for lists of numbers.
  2. Develop a program which takes a list of numbers and returns the length of the list, i.e., a count of the items in the list. Consider: How many numbers are in (cons 3 (cons 1 empty)), for example?
  3. Develop a program which takes a list of numbers and returns the sum of all the numbers.
  4. Develop a program which takes a list of numbers and returns the product of all the numbers.

A Database

To do: First, copy the following into a file db.ss.

     ;; Some basic constants: 
     (define MAX-SALARY 1000000)
     (define MIN-SALARY   20000)

     (define MIN-AGE 18)
     (define MAX-AGE 65)

     ;; A database record is
     ;;   (make-record symbol a s)
     ;;   where a is an integer between MIN-AGE and MAX-AGE (inclusive)
     ;;   and s is an integer between MIN-SALARY and MAX-SALARY (inclusive)
     (define-struct record (name age salary))

     ;; A database is a list of database records, i.e., one of
     ;;   - empty
     ;;   - (cons f r)
     ;;     where f is a database record, r is a database
  1. Develop db-count, which takes a database and returns a count of those employees who are older than 22 and earn more than 100,000.
  2. For the curious... Develop db-search, which takes a database and returns a list of those same employees (in the order they appear in the database).
  3. For the curious... Save (as text!) your solution for db-search, in the same file db.ss. Copy the file db into the same directory. Run (from an xterm window) the program db, e.g.,
         % db
         Man    23 120000
         BigMan 46 800000
         Little 19  30000
         Woman  32 300000
         Kiddo  18  20000
         
    That's just a simple example to show you that Scheme programs can be executed from the Unix prompt (on Owlnet), rather than in DrScheme. They can also be run directly in Windows and MacOS, but we won't get into those details here.


HTML library

To prepare for assignment 3, read the overview of the HTML library. For each of the following, look at only the individual data definitions needed by each problem.

To do:

  1. Create at least one html-object. Create a HTML file for each using produce-test-page. Look at the file with your browser.
  2. Create at least one list-of-html-object. Create a HTML file for each using produce-test-page/list.
  3. Create at least one html-page. Create a HTML file for each using product-to-file.