On this page:
Partner Up!
Lists
Adding Structure
Before You Go...
7.5

Lab 5 Lists

lab!

Purpose: The purpose of this lab is to you some hands-on experience with lists, lists of structures, and structures of lists.

Textbook references: Chapter 8: Lists, Chapter 10: More on lists

Partner Up!

Goals: Get a lab partner. Begin a beautiful friendship.

As you walk into the lab you should see a list on the board of partnerships. Please find your assigned partner and sit with them. This person is your partner for the next 4 weeks. Say hello! Ask them their favorite color! Tell them something wonderful that happened to you this week!

Exercise 1 Exchange contact information with your partner. You will need to get together a lot so you are going to need to be able to talk to each other. Please give them a method of communicating with you that you will check regularly (as in, don’t just give them your email if you never check your email).

Exercise 2 Arrange a time to start working on homework 5 with your partner. It’s due on Thursday so you are going to have to do it soon. It would be a good idea to establish a time to work on future homeworks as well, if you can.

Exercise 3 Read the policy page to find out what to do if you are having problems with your partner. It is very important that you tell us if you are struggling to work with your partner. Otherwise we can’t help you resolve the problem.

Lists

For each of the following exercises, be sure you design any data definitions you need before you begin working on the function(s).

Exercise 4 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 5 Design the function average which takes a list of Numbers and returns their average. What should you produce when the list is empty?

Exercise 6 Design the function odd-true? which takes a list of Booleans and returns true if there are an odd number of trues (#t) in the list.

design any data

Consider the following data definition:
; A Pet is one of:
; - "cat"
; - "dog"
; - "snake"
; - #f
; and represents a pet that someone owns (where #f is 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 ListOfPets.

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!

Adding Structure

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.

Before You Go...

If you had trouble finishing any of the exercises in the lab or homework, or just feel like you’re struggling with any of the class material, please feel free to come to office hours and talk to a TA or tutor for additional assistance.