8.15

Homework 2🔗

home work!

Programming Language BSL

Due Date: Wednesday January 22, 9pm

Purpose To design small programs and practice using enumerated data.

Expectations

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:

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.