Programming with
Data Structures and Algorithms

Finger Exercises: Part 2

Complete the following exercises. Don't forget to write check-expects.

  1. Develop a datatype for family trees, where a family tree is either unknown or a Person, where a Person has a name (string), eye color (symbol), mother (family tree) and father (family tree).
  2. Write a function count-persons that consumes a family tree and produces the total number of people in that tree.
  3. Write a function count-gens that consumes a family tree and produces the number of generations in that tree.
  4. Write a function get-eye-colors that consumes a family tree and produces a list of all the eye colors that appear in that tree (duplicates are ok).

    Hint: use append, which consumes any number of lists and concatenates them.
  5. For the following problems, use higher-order functions

  6. Write a function remove-below-ten that consumes a list of numbers and produces a list with all the numbers below ten removed.
  7. Write a function count-persons that consumes a family tree and produces the total number of people in that tree.
  8. Write a function say-hello-to-all that consumes a list of strings and produces a list of strings with the string "Hello, " added to the front of each.
    1. Create a struct Person that contains a string name and non-negative integer age
    2. Write a function sort-by-age that takes a list of people and sorts them from oldest to youngest. Now, youngest to oldest (sort-by-age2).
    3. Write a function sort-by-name that takes a list of people and sorts them alphabetically by name.
  9. Write a function sum-ages that accepts a list of Persons and sums the ages in the list.
    1. You are planning a party and want to invite a group of Persons. Write a function that accepts a list of Persons and determines whether you can serve alcohol at the party (are all Persons in the list at least 21? Call the function all-over-21?)
    2. You realize you don't actually care if everyone is at least 21, because all you need is at least one person to be 21 for alcohol to be present (and consumed only by those above 21, of course). Write a function that accepts a list of Persons and determines if there is at least one person above the age of 21. Call this function at-least-one-over-21?.

When you are done...

Call a TA over to come check your work. If you get checked off, you are free to go!