On this page:
2.9.1 Objectives
2.9.2 Exercises (due 2011/  10/  12 08:  45:  00)
2.9.3 Optional Exercises (due 2011/  10/  17 08:  45:  00)
2.9.4 Notes

2.9 Introduction to Lists

A train is either

(p.s. a train is a list)

a car or caboose

2.9.1 Objectives

At the end of this class, you should know:
  • what is meant by a list

At the end of this class, you should be able:

2.9.2 Exercises (due 2011/10/12 08:45:00)

1. Write a data definition for a list of numbers. Write product that consumes a list of numbers and produces the product of the numbers.

2. Use the data definition for lists of strings from class and create a list of colors that contains at least 5 colors.

3. Use your list of numbers data definition and write a function called containsFiveHuh that returns true if the number list contains 5.

4. Use your list of numbers data definition and write a function called allEvenHuh that returns true if the number list contains only even numbers.

5. Make a data definition of lists of booleans. Write a function called allTrueHuh that is true when all the booleans in a list are true.

6. Use your list of strings data definition and write a function called longestColourLength that returns the length of longest color in the the list. (max and strlen may be useful.)

2.9.3 Optional Exercises (due 2011/10/17 08:45:00)

7. Using your list of numbers, write numLength that finds the length of a list of numbers.

8. Using your list of strings, write containsRed that returns true if "red" is in the list of strings. (Remember about streq)

9. Using your list of booleans, write anyFalse that returns true if a list of booleans contains any false booleans.

2.9.4 Notes

These notes are primarily for my sake, but I don’t see any reason to hide them from you.

Compounds are for a known number of things, what if we don't know how many there are?

 

How could we break down any number of things into a mixed data definition?

 

We'd know what to do if there was 0, if there was 1, etc

 

How could we get any number? 1 More! And then some...

- this is a structure reference just like we've seen before

- but a little funny, because it is a reference to the same thing

 

Try list of planets (strings)

 

Write length

=> num

 

Show strlen

 

Write sumLength

=> num

 

Write averageLength

=> sum / len

 

Write containsLessThan4

=> bool