COMP 210 Lab 3: Lists

This lab is mainly practice with 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. We may revisit some of these examples in next week's lab, also.

Important for all examples:

Index: A new flavor of define, Definition and Design Recipe Review, Lots of Examples


A new flavor of define

Labby note: Just skim this section.

We saw in lecture a new way to use define. Our original usage, if you think about it, really does two separate things:

     ;; circle-area: number -> number
     (define (circle-area r)
       (* pi (* r r)))
First, a new function is created (it takes in a number and returns a number); Second, a name is associated with that function ("circle-area").

The new way to use define actually is simpler: it simply associates a name with a value (not (necessarily) a function).

     (define speed-limit 55)
     (define international-greeting 'hello)
     (define my-favorite-number (+ 15 2))
     (define my-favorite-ppg (make-ppg 'Buttercup 'green 90 ))
Some people call these names variables, though they never actually vary. We'll call them placeholders.

All placeholders do is save us typing. Whereever in a program you see speed-limit, you could conceivably re-write the program to use 55, and the program's behavior will be the same. Thus it's a handy way to save typing of structures (like ppgs, which are tedious to type). Nothing else mysterious happens.

Note that use of placeholders is important in both

For discussion
  • How do these principles apply to a program using "55" vs. a program mentioning speed-limit?
  • If you use a such a number only once, should you use the number directly, or define a placeholder for it? Do the two principles still apply?

For constants like 0, 1, true, false, and empty, it generally doesn't make sense to define separate placeholders.

These two uses of define are certainly related -- the function version does extra work of creating a function, but after that they are identical: If you think of functions as just being another type of value (in addition to numbers, symbols, Booleans, and structures), then in either case, a placeholder is being associated (bound) to a value.

Note for the curious
We'll see later how to create a function without needing to give it a name.

Summary of define syntax

define question

How does the computer tell these two cases apart?

Hint: Pay attention to which parentheses belong to the define, and which are actually part of various sub-expressions.


Definition and Design Recipe Review

We'll go over the lecture notes, actually typing in the examples presented there. The goal is to be comfortable with the following:

Revised Design Recipe

Now, let's quickly review our latest design recipe. We know our programs should take advantage of the structure of the data; recursive data implies recursive functions.

Steps depending only on the data:

Steps depending on the function:

An English aside
"To recur" is what programs do; "to recurse" is what you may sometimes do in frustration.

Lots of examples

There are many more examples here than you can do in lab. Labbies can pick and choose what to present. Students should look over the rest on their own.

Lists of numbers

Series of list-of-numbers exercises
Do at least the first five parts in lab:
  1. Make the data definition for list-of-numbers. (Extremely similar to list-of-symbols.)
  2. Make some examples of list-of-numbers.
  3. Make the template for list-of-numbers. Make sure you understand what each step of the design recipe wants.
  4. 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. (Of course, also first make some test cases, e.g., how many numbers are in (cons 3 (cons 1 empty))?)
  5. Once your function works, use the stepper, and step through the program on a list of length 2. How many times is your function called (counting both the initial call plus recursive calls)?
  6. Develop a program which takes a list-of-numbers and returns the sum of all the numbers. (Yes, this includes test cases. We'll stop reminding you now.)
  7. Develop a program which takes a list-of-numbers and returns the product of all the numbers.

Databases

We've seen lists of symbols and lists of numbers; you can of course have a list with elements of any given type ... including other structures like villains. This just uses ideas you've already learned, but in new contexts. Stick to the design recipe, to avoid confusion!

Series of database exercises
  1. Copy the following into DrScheme:
             (define-struct villain (name power always-hungry? iq weakness))
             ;;
             ;; A villain is:
             ;;   (make-villain [symbol] [number] [boolean] [number] [symbol])
             ;; where power is the power-rating, iq is an IQ.
             ;; weakness describes the villain's achilles heel.
             ;; name and always-hungry? are self-evident.
             
             ; Examples of data:
             (make-villain 'fuzzy-lumpkins 90 true 35 'witty-banter)
             (make-villain 'mojo-jojo 35 false 190 'soliliquizing)
             
    
             #|
             ;;;; TEMPLATE
             ;; villain-handler: villain --> ??
             ;;
             (define (villain-handler a-villain)
                ...(villain-name a-villain)...
                ...(villain-power a-villain)...
                ...(villain-iq a-villain)...
                ...(villain-always-hungry? a-villain)...
                ...(villain-weakness a-villain)...  )
             |#
             
  2. Create some more example villains.
  3. Write the function dangerous?, which takes in a single villain, and returns true exactly when the villain has an IQ over 190, or a toughness over 50. (Hint: The English description uses "or"; your code may use or!)
  4. The mayor wants you to keep a crime-database. Hmm, what data type is appropriate, to represent a crime-database?
  5. Write the template for functions which handle entire crime-databases.
  6. Develop number-dangerous, which takes a crime-database and returns a count of those villains who have an IQ over 190 or a toughness over 50. (Hint: Remember the principle of not repeating code!)
  7. For the curious... Develop dangerous-names, which takes a database and returns a list of those employees' names. Follow the template!