Lab 5 Self-referential data; Lists of atomic and structured data
Purpose: The purpose of this lab is to practice using lists of "all kinds of data", including atomic and structured ones.
Warm-up: Recalling the pedestrianness of self-referential data
Coach Matt is the coach of a very successful relay race team. Each member of the team takes turns completing parts of the race before passing the baton to the next member. Each runner has a name and a fastest time.
Exercise 1 Design data Runner for a runner, and a constant TIM for Tim, the fastest runner. Then, based on that, design data Rrt for a relay race team. Such a team always contains "Tim", with a relay time of 10 (in sec). The team may further have other members.
Here’s something to get you started:
(define-struct runner [name time]) ; ... A Runner is ... (define TIM (make-runner "Tim" 10)) ; this will appear in code, hence in CAPS (define-struct rrt [runner rrt]) ; An Rrt is one of: ; - TIM ; - (make-rrt Runner Rrt) ; ...
Exercise 2 Design a function that computes an Rrt’s total race time, assuming all the members run their fastest time.
Exercise 3 A clock skew has been detected! Design a function that takes an Rrt and decreases all of its runner’s times by 1 second.
Exercise 4 Design a function that takes an Rrt and a number and removes all runners slower than the given time, unless it is Tim (who is on every Team).
Lists of atomic data
For each of the following exercises, be sure you design any data you need before you begin working on the function(s).
Exercise 5 Design the function contains-string?, which takes a list of Strings and a String and returns #t if the given string is in the given list.
Exercise 6 Design the function average, which takes a list of Numbers and returns their average. What should you return when the list is empty?
Exercise 7 Design the function odd-true? which takes a list of Booleans and returns true if there are an odd number of true’s (#t) in the list.
Lists of structured data
Consider the following data definition:
; A Pet is one of: ; - "cat" ; - "dog" ; - "snake" ; - #f ; and represents a pet that someone owns; #f represents any other kind of pet (define pet-c "cat") (define pet-d "dog") (define pet-s "snake") (define pet-o #f) ; pet-template : Pet -> ??? (define (pet-template p) (... (cond [(and (string? p) (string=? p "cat")) ...] [(and (string? p) (string=? p "dog")) ...] [(and (string? p) (string=? p "snake")) ...] [(boolean? p) ...])))
Exercise 8 Design data for a list of pets.
Exercise 9 Design the function all-noises which takes a list of Pets and produces a list of Strings representing the noises they make. A dog barks, a cat meows, a snake hisses, and any other animal will just produce "unknown". Be sure to follow the template you designed earlier!
Now consider the following data definition:
(define-struct owner [first last pets]) ; A PetOwner is a (make-owner String String ListOfPets) ; - where first is the first name of the person ; - last is the last name of the person ; - and pets is the type of pets the person owns (define owner-none (make-owner "Abigail" "Alone" '())) (define owner-all (make-owner "Billy" "Bungalow" (cons "cat" (cons "dog" (cons "snake" (cons #f '()))))))
Exercise 10 Define the template for a PetOwner.
Exercise 11 Design the function do-they-own-it? which takes a PetOwner and a Pet and returns #t if the PetOwner owns the given pet.
Exercise 12 Design data for a list of pet owners.
Exercise 13 Design the function most-pets, which takes a list of PetOwners and returns the maximum number of pets that anyone in the list owns.