On this page:
Partner Up!
Self-Referential Data Example
Self-Referential Data in the World
7.9

Lab 4 Partners, Unions, and Self-Referential Data

lab!

Purpose: The first purpose of this lab is to assign partners. We will then practice designing and programming with unions and self-referential data.

Partner Up!

Your lab staff should have assigned you to a partnership. You can find this on the handins server. You and your partner will be working together in labs and on homeworks from now until approximately Lab 8. Since you and your partner are randomly assigned, so we understand sometimes things don’t work out. We will help in such situations.

Some guidelines on pair work and etiquette:

You are required to work together on all homeworks you submit. Not doing so is plagiarism, and is a violation of our academic integrity rules.

If you have trouble with your partner, document the issues you are having and reach out to your lab’s head TA if you can’t resolve the issue on your own.

We will step in to handle cases where:

If you find yourself in any of these situations, please begin immediately documenting your situation. If it becomes more than a one-time issue, email your head TA and we will try to find an appropriate solution.

Remember, using the internet to research solutions and collaborating with other groups is encouraged. Copy + pasting code that is not your own is cheating.

Exercise 1 Exchange contact information with your partner. You both need to agree on a reliable method of communicating.

Exercise 2 Arrange a time to start working on Homework 04 with your partner (although right now it’s not posted yet). This will be the first partner homework, and it’s due on Friday, February 19th.

Remember: the idea is not to split the homework up exercise by exercise. This approach will hurt you in the long run when you get to the exam and don’t understand core concepts taught by exercises that your partner did without you.

Self-Referential Data Example

Exercise 3 Matryoshka dolls, or nesting dolls, are simple example of self-referential data. Go ahead and google them if you haven’t seen one before. Design a data definition for a nesting doll which includes both the innermost doll, and the rest of the dolls.

Because the innermost doll is separate in that it has no doll inside of it, we will represent it as a string of its color.

Exercise 4 Design a simple function double that takes a nesting doll and produces a new nesting with each layer doubled.

For example, a red doll wrapping a blue doll wrapping a yellow doll at the core would become: red, red, blue, blue, yellow, yellow at the core.

Self-Referential Data in the World

Your friend, a psychology major, is trying to perform hypnosis on another student. He thinks some great visualization will go a long way. He has asked you to develop a program that shows a set of concentric rings of increasing radius and various colors filling a quadratic screen. The program will start with a blank screen. Each time the clock ticks it will add a new circle of a random color and increased size underneath the current set of circles, so that only the ring is visible. The program should stop when the current (= outermost) circle has a diameter that exceeds the screen side length.

Here is a video showing what we are looking for:

hypnosis program

Exercise 5 "What stays the same?" Define some constants to represent the things in the world that don’t change.

Exercise 6 "What changes?" We will represent the state of this world using something we call a RingSet, i.e. a collection of rings. A ring should at least have a color and a size. Design data using a self-referential data definition to construct a RingSet. Follow all steps of the data design recipe.

Exercise 7 "Which handlers do we need?" Write the signature and purpose statements for the handler functions you will need. Recall that big-bang always requires a to-draw clause. Which other clauses will you need based on the program description?

Exercise 8 Design the main function hypnosis which, given an initial RingSet, runs the hypnosis program using big-bang.

Exercise 9 Design the function that draws the RingSet onto the screen. NOTE: You will need a helper function here to do the majority of the work. When writing this helper function, the empty-image will come in handy.

Exercise 10 Design the function that updates the RingSet every time the clock ticks. Since random produces a number and not a color, here is a function that converts a natural number in the range [0,7) to a color.

; choose-color : Nat[0,7) -> Color
; Choose the color corresponding with the given number
(check-expect (choose-color 0) "red")
(check-expect (choose-color 1) "orange")
(check-expect (choose-color 2) "yellow")
(check-expect (choose-color 3) "green")
(check-expect (choose-color 4) "blue")
(check-expect (choose-color 5) "purple")
(check-expect (choose-color 6) "pink")
(define (choose-color n)
  (cond [(= n 0) "red"]
        [(= n 1) "orange"]
        [(= n 2) "yellow"]
        [(= n 3) "green"]
        [(= n 4) "blue"]
        [(= n 5) "purple"]
        [(= n 6) "pink"]))

Can you figure out how to use this function to produce a random color?

Exercise 11 Design the function that determines if the program should stop. Remember that the program ends when the current (= outermost) circle has a diameter that exceeds the screen side length.