Homework 2
Due Date: Wednesday January 22, 9pm
Purpose To design small programs and practice using enumerated data.
Expectations
You should submit three .rkt files, each containing your response to one of the 3 exercises below, via the Handin Server; the three assignments will all begin with "HW2-". We accept NO email submissions. Failure to submit a .rkt file will result in a 0.
You are only allowed to use the language specified at the top of this page: failure to do so will result in a 0.
You are only allowed to use the kinds of data definitions that have been covered in class.
Your code MUST conform to the guidelines outlined in the style guide on the course website. The style guide may be updated as the semester progresses so please remember to read it before submitting each assignment.
Unless otherwise stated, all data and functions you design must be designed according to the data and function design recipes (DR), resp., following all four steps in each case, with the exception of functions that call big-bang, for which you need no check-expects, as justified in class. All functions used inside big-bang must have check-expects, along with the other three steps of the design recipe.
We will not walk you through the DR steps in each problem, nor request them individually and explicitly. Instead, review the data and function design recipes before starting this homework, and then have these recipes handy.
Graded Exercises
Exercise 1 Design the function divides? that takes two positive integers and determines if they divide evenly (as in, the remainder upon division is zero). Hint: note that there is no constraint on the order in which the numbers are supplied.
Exercise 2 Consider the following data definition and interpretation (based on this information):
; A MauriceSendakBook is one of ... ; - "Chicken Soup with Rice (1962)" ; - "Where the Wild Things Are (1963)" ; - "Higglety Pigglety Pop! (1967)" ; - "In the Night Kitchen (1970)" ; - "Seven Little Monsters (1977)" ; Interpretation: the name and year of books authored by Maurice Sendak
Define three distinct examples of a MauriceSendakBook.
Write the template for a function that takes in a MauriceSendakBook.
Design the function next-book that takes in a MauriceSendakBook and returns the next most recent MauriceSendakBook (by year); if the function receives the most recent book it should return the oldest book.
Design the function prev-book that takes in a MauriceSendakBook and returns the next oldest MauriceSendakBook (by year); if the function receives the oldest book it should return the most recent book.
Design a World program that allows the user to press the arrow keys to "scroll" through Maurice Sendak books: pressing left goes to the previous book (by year; or the most recent if currently displaying the oldest), right goes to the next book (by year; or the oldest book if currently displaying the most recent). The program should display the current book as black text. Recall that you will need to use (require 2htdp/universe) to require the big-bang library.
The following exercise deals with the Collatz conjecture. In short, the Collatz conjecture says that a particular sequence of numbers, no matter the starting positive integer, will always end in the number 1. After the starting number, each subsequent term is determined as follows: if the previous number was even, the next number is one half the previous number; otherwise, the next number is three times the previous number plus one. For example, if the first number is 12:
12 is even, so next: 12 / 2 = 6
6 is even, so next: 6 / 2 = 3
3 is odd, so next: (3 * 3) + 1 = 10
10 is even, so next: 10 / 2 = 5
5 is odd, so next: (3 * 5) + 1 = 16
16 is even, so next: 16 / 2 = 8
8 is even, so next: 8 / 2 = 4
4 is even, so next: 4 / 2 = 2
2 is even, so next: 2 / 2 = 1 (done!)
Exercise 3 Your goal is to design a program that tests the Collatz conjecture.
To begin, design the function next-collatz that takes a positive integer and produces the next number in the sequence.
Now, design a function collatz that visually unfolds the sequence, starting at a supplied positive integer. At each step your program should show the current number, whether it is even/odd, and the arithmetic that leads to the next value (as exemplified above). The program should allow the user to read the text about each number in the sequence, and then press a key to see the next. If the Collatz conjecture holds (meaning, the term results in 1), the program should end and show a screen that acknowledges this fact.