Note: For groups that may have a significant amount of programming experience, your PM may suggest trying out the Advanced Tutorial 3

Submitting

Regardless of how you choose to complete the assignment, you MUST submit the file you worked on to Canvas.

If you’re submitting remotely, your submitted file will be graded for completeness and correctness via an autograder. Make sure your functions are named correctly and that all of the built-in check-expects pass.

If you’re submitting in-class, make sure to check-in (there will be one question called CHECK-IN Q only available for the first 7 minutes of class) and check-out (there will be a three questions survey called CHECKOUT SURVEY only available for the last 10 minutes of class) using PollEverywhere with your location verified.


Getting Started

For this tutorial, you’ll experiment with lists and structs. Make sure to write check-expects for each expression you write to test its correctness.

There a number of goals here:

  1. Get some practice using composite data (both lists and structs)
  2. Get a lot of practice manipulating lists using those library of list functions we talked about in Lecture 9
  3. Practice thinking about and writing our OWN tests for our programs.

Tutorial 3 Starter Files

Alright, now that you’ve read the goals and downloaded the template files, let’s get started!


Part 1. Iterating Over Lists of Lists

Let’s start by experimenting with lists and the built-in iterators map, foldl, and filter.


Activity 1

Define a list of numbers and call it num-list. The numbers in the list should be 2, 3, 4, 5, and 6.

BEFORE YOU DO SO, go ahead and write a check-expect (that’s right, we’re going to write a test BEFORE we write a program). In this case, we don’t care WHAT is in this list…just that it’s a list, so we’ll use the list? predicate to make sure the thing we make actually is a list. We’ll give you this one, but from now on, you’re going to have to write your own:

(check-expect (list? num-list)
              true)

Activity 2

Now use map to write a function, call it list-times-5, that takes as input a list and multiplies each number in that list by 5, and returns the result. Write a check-expect that uses num-list as an example input to your function to make sure that given whatever values you put in num-list, your function is returning the same list but multiplied by 5. For example, if you had used (list 0 1 2 3), then you’d be expecting to get back (list 0 5 10 15).

Forgotten the type signatures for the list functions? You can use the lecture slides as a reference or start getting used to our Quiz Glossary which lists these functions.


Activity 3

Now use filter to write a function, call it activity-3, that finds all the numbers that are either equal to 2 or greater than 5. Don’t forget to write some tests to try your function on! Make sure to test it on your num-list


Activity 4

Next use foldl to write a function, call it my-sum, to compute the sum of the numbers in a list. Check it against the sum of your num-list.

Hint: the sum of the empty list (a list with no elements) is just zero.


Activity 5

And now we are going to use andmap to determine whether all the numbers in a list are less than three. Write a function that does this, call it all-less-than-three?, and make sure to test it on your num-list list.


Activity 6

Next try and see if you can find another function to determine whether any of the numbers in a list are less than three. Make a new function called any-less-than-three? and write a test using at least your num-list.


Part 2. Building structures

Instead of using the built-in type lists, we can also create composite data via structs.

structs Refresher You can define a new data type in ISL+ by using the define-struct special form:

(define-struct type-name (field-names ...))

type-name is the name to give to this new kind of data and field-names are the names to give to the different bits of information you want to keep track of in this new data type. So, for example, if we wanted to keep track of information about employees of a company, specifically, their first name, last name, and social security number (SSN), we might write something like this:

(define-struct employee (first-name last-name ssn))

When we run that, ISL+ defines a number of functions for us automatically:
  • (make-employee first-name last-name ssn) makes a new employee object with the specified info stored inside it.
  • (employee? object) is a predicate that tests if object is specifically an employee object or some other kind of data.
  • (employee-first-name e) takes an employee object e and returns the first-name stored in it.
  • (employee-last-name e) takes an employee object e and returns the last-name stored in it.
  • (employee-ssn e) takes an employee object e and returns the ssn stored in it.

Note: those last three functions are sometimes referred to as getters or accessors because they help us extract different property values from some given object

Activity 7

Suppose you want to represent information about cats, because cats are purrfect. Specifically, you want to represent their name, their breed, their hair color, and the volume of their meow. Write a define-struct expression for a new data type, cat, that contains the fields name, breed, color, and meow-volume.

Internal Monologue: If a cat taught this class, would they be called a Purr-fessor? Should I stop now?


Activity 8

Now pick a name, breed, color, and meow volume for a cat (the first three should be strings and the last one should be a number between 0 and 10 inclusive) and make a cat object to represent it. Store it in the variable my-cat.

What’s a cat’s favorite TV show? Claw and Order.

While we’ve given you check-expects to verify the type of your cat’s name, breed, and color, you’ll need to write 2 check-expects to verify the meow-volume field.


Activity 9

Now write expressions to get or extract the name and breed you gave the cat object stored in my-cat. Store them in got-the-name and got-the-breed respectively.

And you’re done with this part! I bet you thought this part was gonna be a cat-astrophe…


Part 3. More List Manipulation

Activity 10

We’ve included a list of lists of numbers in a variable called lst-of-lsts. Write a function to compute the product of all the numbers in all its sublists and call it lists-product.

We’ve included a commented out input and expected output you can turn into your own check-expect.


Activity 11

We’ve put a list of strings in your Racket file called word-list. Write a function, call it first-letter-word-maker, to append together the first character of each string in the list.