Homework 2

home work!

Programming Language BSL

Due Date Tuesday 05/12 at 10pm

Purpose To write simple functions & tests relating to numbers, images, strings, enumerations

  • You should submit a single .rkt file containing your responses to all exercises via the Handin Server. 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.

  • 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.

  • You must follow the Design Recipe.

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 code:
; 1-or-0 : Boolean -> Number
; Output 1 if b is true, else 0
(check-expect (1-or-0 true) 1)
(check-expect (1-or-0 false) 0)
(define (1-or-0 b)
  (if (if b true false) 1 0))
Explain in a comment why (if b true false) is unnecessary and rewrite the function accordingly.

Exercise 3 Consider the following data definition (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.

  • Design 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 4 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 acknowledges this fact.