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.
Submission: Submit your solutions to Exercises 5, 6, 7, 8 and 9 to handins. You should work with your homework/lab partner on the exercises and make one submission for your team.
Pairs Programming: We are trying to reinforce optimal pair programming practices in these labs. An important part of pair programming is that the same person isn’t always typing. For this lab, you should switch off roles between each exercise.
Code Review: We are initiating code reviews with this lab. The TA will talk to each student to make sure that every individual has a basic understanding of what is going on in this lab. At some point after you have finished the first two exercises (a data design and a function design), you should have a TA come over and review your work. You should be able to explain, in no more than a minute or two, how your code works and why you made the choices you made. You and your partner will be explaining to separate TAs at the same time, so make sure you know, yourself, what you did and why: your partner will not be able to help you explain.
This code review will occur concurrently with the rest of the lab; after you have indicated to the TAs that you and your partner are ready for your code reviews, you should continue work on the remainder of the lab.
Warm-up: 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)) (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 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 7 Design data for a list of pets.
Exercise 8 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 9 Define the template for a PetOwner.
Exercise 10 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 11 Design data for a list of pet owners.
Exercise 12 Design the function most-pets, which takes a list of PetOwners and returns the maximum number of pets that anyone in the list owns.