On this page:
New Partners!
Getting an A
Play It Again
All Hands On Duck
6.6

Lab 7 Existing Abstractions

home work!

Purpose: In this lab we will practice using existing list abstractions.

Textbook References: Chapter 16.1: Existing Abstractions

Throughout the lab you must use existing list abstractions where appropriate. Try to pick the best list abstraction for the job: you will need practice with all of them.

New Partners!

We are allowing you to request partners for this next round of partnership. You can request exactly ONE partner (we will not allow groups of three unless we find that students are working alone). The instructions on how to request a partner on the handin server are available here.

Exercise 1 Request a partner on the handin server if you know who you would like to work with. It should be someone you have NOT worked with already and someone who is in your lab section. If you do not have anyone in mind, please notify your TA now so that they can assign you a partner as soon as possible.

You will work with your new partner on future labs and complete assignment 8 together. Note that assignment 7 should be completed with your old homework partner(s).

Getting an A

Consider the following data definition:

; A Student is a (make-student String Number).
; where the name is the student's full name, and the grade is a percentage,
; represented as a number between 0 and 100 inclusive.
(define-struct student (name grade))

Exercise 2 Design the function give-everyone-extra-credit which takes a [List-of Student] and gives every student some number of points of extra credit. You get to decide how many points. So much power! If adding the extra credit would give the student a grade greater than 100 you should give them a 100 instead.

Exercise 3 Design the function struggling-students which takes a [List-of Student] and returns a list of only the students whose grades are at most 70.

Exercise 4 Design the function none-absent? which takes a [List-of Student] and returns #true if all the students in the list have a grade of more than 0.

Here is a data definition for a non-empty list:

; A [NonEmptyList-of X] is a (cons X [List-of X])

Exercise 5 Design the function best-student which takes a [NonEmptyList-of Student] and produces the student who has the highest grade in the list. If multiple students have the same grade the function can produce any one of them. WARNING: Your function should return a Student, not just the student’s grade.

Exercise 6 Design the function any-same-name? which takes a [List-of Student] and returns #true if any Student in the list has your name. You can fight with your partner about whose name you want to check for. Or you can check for both!

Play It Again

Consider the following data definitions:
(define-struct playlist [title all-songs])
; A Playlist is a (make-playlist String [List-of Song])
; - where title is the title of the playlist
; - and all-songs is the list of the songs in the playlist
 
(define-struct song [title artist genre len])
; A Song is a (make-song String String String PositiveInteger)
; - where title is the title of the song
; - artist is the name of the artist who sings/plays the song
; - genre is the genre of the song
; - and len is the song's length in seconds

Exercise 7 Design the function total-playlist-length which takes a Playlist and produces the total length of the playlist in MINUTES.

Exercise 8 One of your TAs only wants to listen to music from Disney soundtracks. Design the function rename-if-all-same which takes a Playlist. If all the songs in the Playlist have the genre "Soundtrack" the function returns the given Playlist with the title "Disney". Otherwise it returns the given Playlist without any changes.

Exercise 9 One of your TAs wants to play music for the lab but they don’t want to play any songs you don’t like. Design the function remove-all-by-artist which takes a Playlist and returns a Playlist where all songs by an artist you don’t like have been removed. I won’t tell you which artists you should not like because music tastes differ wildly. Pick someone. Pick your partner’s least favorite artist if you like. Pick both if you like. So many choices.

Exercise 10 When displaying music we want to show an abbreviated song title if the title is too long. Design the function abbreviate-long-titles which takes a Playlist and returns a Playlist where any song that has a title longer than 20 characters has a new title that is the first 30 characters of the title with "..." at the end. For example the song titled "Do You Want To Build A Snowman" would now have the title "Do You Want To Build...".

Exercise 11 Design the function any-long-songs? which takes a Playlist and returns #true if any songs in the Playlist are longer than 3 minutes.

All Hands On Duck

When one of your friends was finished playing with her extensive collection of rubber duckies, she pulled out the bathtub drain and watched as each duck swirled closer to the drain and eventually disappeared down it. "I could make big-bang do that!" she thought excitedly. But she needs your help to write the program.

Step 1: What stays the same?

Exercise 12 Define some constants to represent what stays the same in the world. For example you may want to find images online of a cute rubber duck and a menacing whirlpool as those are both images that you will need to use to draw your program. What other constants can we define?

Step 2: What changes?

Exercise 13 Develop a data definition for your program. You will need to keep track of an arbitrary number of ducks, each with an x and y position on the screen.

Step 3: Which handlers do we need?

As usual we will need a to-draw handler, since that is mandatory for all big-bang programs. In this program the ducks move towards the center gradually over time, so we will also need a on-tick handler. We will split this handler up into several helpers which we will cover in the next section.

Step 4: Design your handlers

Exercise 14 Design your to-draw handler.

Your TA remembered enough about fluid dynamics to produce the following functions:

; go-with-the-flow : Posn -> Posn
; produces a new posn that is the next point on a
; roundabout path to the CENTER
(define (go-with-the-flow p)
  (if (and (= (posn-x p) (posn-x CENTER))
           (= (posn-y p) (posn-y CENTER)))
      CENTER
      (make-posn (+ (posn-x p) (* SPEED (cos (find-angle (- (posn-x p) (posn-x CENTER))
                                                         (- (posn-y p) (posn-y CENTER))))))
                 (+ (posn-y p) (* SPEED (sin (find-angle (- (posn-x p) (posn-x CENTER))
                                                         (- (posn-y p) (posn-y CENTER)))))))))
 
; find-angle : Number Number -> Number
; Find the angle to travel at when moving from one position to another
(check-within (find-angle 50 2) 2.3962 0.001)
(check-within (find-angle 1 100) -2.3662 0.001)
(define (find-angle dx dy)
  (atan (- dx dy) (- 0 dx dy)))

As you can see, the TA is using several constants which you will have to define in order to get the program to work correctly.

Exercise 15 Design the function move-all-ducks which takes in your worldstate and produces a new worldstate where the ducks have moved one step closer to the drain. The go-with-the-flow function will be useful to you here. Which list abstraction does the same thing to every element of a list?

Exercise 16 Design the function safe-from-drain which takes in your worldstate and produces a new worldstate with only the ducks that are not at the same position as the drain. Which list abstraction can remove elements from a list?

Exercise 17 Design the function replenish-ducks that takes in your worldstate and a NaturalNumber. If there are already at least that many ducks, the function does nothing, but if not it adds more ducks until there are the given number of ducks. These new ducks should be placed at a random position anywhere on the screen.

Exercise 18 Design the function update-ducks which uses move-all-ducks, safe-from-drain, and replenish-ducks to move the ducks, remove any ducks that have gone down the drain, and replenish the world to have the same number of ducks as it did originally. This function will be your on-tick handler.

Step 5: Put it all together!

Exercise 19 Design a function that uses big-bang to create your program, inserting the functions you defined above into the appropriate clauses. Your program should take as input the numbler of ducks to initially display on the screen.