Comp 210 Lab 3: More Lists

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. Last week's list examples are repeated here, since many of you didn't have enough time to work on them last week.

Important for all examples:


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.

Database

Let's build an example using lists of more interesting data.

To do: First, copy the following into DrScheme:

     ; A database record is
     ;   (make-record name age salary)
     ; where name is a symbol, and age and salary are positive numbers
     (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. Create an example database.
  2. Develop db-count, which takes a database and returns a count of those employees who are older than 22 and earn more than 100000.
  3. 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).

Non-empty lists

To do:

  1. Make a data definition for non-empty lists of numbers. Hint: The base case should not be empty, since that is not a non-empty list of numbers! What is a description of the shortest non-empty lists of numbers?
  2. Develop a program which takes a non-empty list of numbers and returns the average (aka, arithmetic mean) of all the numbers.
  3. Develop a program which takes a list of numbers and returns the average of all the numbers. For this example, arbitrarily define the average of an empty list to be false.
Note: There are actually two reasonable solutions to this, although we hinted towards the usually preferable one.

Mixed data (heterogenous) lists

To do:

  1. Make a data definition for a value which is a symbol or a number.
  2. Make a data definition for lists containing symbols and/or numbers. Examples would include
         empty
         (cons 1 (cons 5 (cons 0 empty)))
         (cons 1 (cons 'hi empty))
         (cons 'hello (cons 'there empty))
         
  3. Develop a program which computes the product of all the numbers in a such a list. The structure of your program should correspond with your choice of data definition.
Note: There are two reasonable ways to do this, although we hinted at the usually preferable one.

Functions resulting in lists

To do:

  1. Develop a program which consumes a list of numbers and another number and returns a list of the sums of the original list and the second argument. E.g.,
         (add-numbers (cons 1 (cons 3 (cons 4 empty))) 2)
         =
         (cons 3 (cons 5 (cons 6 empty)))
         
  2. Develop a program which consumes a list of numbers and returns a list of all of those numbers which are positive.