Starting from:
$25

$19

Lab 7 Follow the Signatures

Purpose: This lab will give you practice using existing list abstractions and analyzing abstract functions.
Textbook references: Chapter 14: Similarities Everywhere, Chapter 15: Designing Abstractions, Chapter 16: Using Abstractions
New Partners!
Goals: Make new friends, but keep the old.
By now you should have received new partner assignments. If you haven’t yet, say hello! Ask them their favorite color! Tell them something wonderful that happened to you this week!
Exercise 1 If you haven’t already, exchange contact information with your partner. Arrange a time to start working on homework 7. It’s due on Friday so you are going to have to do it soon. It would be a good idea to establish times to work on future homeworks as well, if you can.
Exercise 2 Re-read the policy page to refresh your memory on 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.
Understanding Abstractions
Goals: Understand the signatures and purposes of each pre-defined list abstraction.
Exercise (Reviewed) 3 Below is the definition of foldr.
(define (foldr combine base lst)
  (cond [(empty? lst) base]
        [(cons? lst) (combine (first lst)
                              (foldr combine base (rest lst)))]))
Determine the signature for foldr from this definition.
Exercise 4 Determine the best pre-defined list abstraction to use for each of the following tasks. Have a staff member look over your answers before you continue by visiting them in their group call.
    • Create a list of the string-lengths of every string in a list.
    • Determine whether any Posn in a list is within a certain area.
    • Given a list of strings, create a list of only the strings that contain only numbers.
    • Check whether every number in a list is within a certain range.
    • Given a list of Posns, create a single Posn whose x-coordinate is the sum of all the x-coordinates in the list and whose y-coordinate is the sum of all the y-coordinates in the list.
Exercise 5 Design the function accumulate-posn which, given a list of Posns, creates a single Posn whose x-coordinate is the sum of all the x-coordinates in the list, and whose y-coordinate is the sum of all the y-coordinates in the list (this is the last example from the previous exercise).
Exercise 6 Determine the signature of the following function:
(define (f x y z)
  (or (andmap string-numeric?
              (map x (filter positive? (foldr append '() y))))
      z))
All Hands On Duck
Goals: Use list abstractions to design a fun world program!
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 eventualy disappeared down it. "I could make big-bang do that!" she thought excitedly. You will help write the program to simulate draining away her arbitrarily large collection.
Your program will take as input a list of the initial ducks to toss first. The simulation will then place those ducks in the tub, and continue to fill the tub to capacity with new ducks at random locations as ducks disappear down the drain. For this program, we will designate that the drain is located at the origin, but we will want to draw it at the center of the window. Keep this in mind as you go through the steps of designing and drawing your ducks.
We’ll break down the process of designing this program into the following parts:
Step 1: What stays the same?
Exercise 7 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 draw your program. What other constants can we define?
Step 2: What changes?
Exercise 8 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 (note: relative to the origin, which is not the same as your on-screen coordinates).
Step 3: Which handlers do we need?
Exercise 9 Write the signature and purpose statements for the handler functions you will need (but don’t implement them just yet). Recall that big-bang always requires a to-draw clause. Which other clauses will you need based on the program description? If you are having trouble, take a look at the documentation, and if you are still confused, ask a tutor or TA in their Teams channel for assistance.
Step 4: Design your handlers
Exercise 10 Design the function that draws the ducks on the whirlpool scene. Remember to use pre-defined list abstractions whenever you can!
The function that moves ducks as the clock ticks is a bit more complicated, so we will break it down into several smaller functions in the following exercises.
Your friend 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
(check-expect (go-with-the-flow CENTER) CENTER)
(check-expect
 (posn-within? (go-with-the-flow (make-posn 100 200)) (make-posn 97.005 199.82) 0.01)
 true)
(check-expect
 (posn-within? (go-with-the-flow (make-posn 321 123)) (make-posn 318.67 124.89) 0.01)
 true)
(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)))))))))
 
; posn-within? : Posn Posn Number -> Boolean
; Are the coordinates of these positions within delta of each other?
(check-expect (posn-within? (make-posn 1 2) (make-posn 2 1) 3) true)
(check-expect (posn-within? (make-posn 100 0) (make-posn 100 0.5) 0.1) false)
(define (posn-within? p1 p2 delta)
  (and (<= (abs (- (posn-x p1) (posn-x p2))) delta)
       (<= (abs (- (posn-y p1) (posn-y p2))) delta)))
 
; find-angle : Number Number -> Number
; Find the appropriate angle to travel at when moving from one position to another
; (This is a little more than 90 degrees rotated from the direction from the CENTER
; to the current position of the duck, if you're curious.)
(check-within (find-angle 50 2) 2.1343 0.001)
(check-within (find-angle 1 100) 3.6551 0.001)
(define (find-angle dx dy)
  (+ (atan dy dx) (/ pi 1.5)))
As you can see, your friend is using several constants which you will have to define in order to get the program to work correctly.
Exercise 11 Design a 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 12 Design the function safe-from-drain which takes in your worldstate and produces a new worldstate with only the ducks that are further than SPEED distance from the drain. Which list abstraction can remove elements from a list?
Exercise 13 Design the function replenish-ducks that takes in your worldstate and a Nat. If there are already at least that many ducks, the function does nothing, but if not it adds more ducks until the tub reaches its capacity. These new ducks should be placed at a random position anywhere on the screen.
Exercise 14 Design the function update-ducks which uses all move-all-ducks, safe-from-drain, and replenish-ducks to move the ducks, remove any ducks that have done down the drain, and refill the tub to capacity (i.e. add more ducks until you 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 15 Design a function that uses big-bang to create your program, inserting the functions you defined above into the appropriate clauses. Your function should take in an initial list of ducks to place in the tub.
Signature Detective
Goals: Practice understanding what the data type restrictions on code must be when reading it.
Exercise 16 Determine the signatures for the following functions:
(define (a supercut of us)
  (+ of
     (if (empty? supercut)
         (us #f)
         (us (first supercut)))))
 
(define (moments i play in the dark)
  (play (in (play the dark)) (play the i)))
 
(define (come home to my heart)
  (cond [(home my) (to heart)]
        [(my heart) " "]
        [else ""]))
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.

More products