6.6

Homework 2

home work!

Programming Language BSL

Due Date Friday, September 20 at 6pm

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

Expectations
  • 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 will be updated as the semester progresses so please remember to read it before submitting each assignment.

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 In each of the parts of this problem you will derive for yourself existing Boolean functions as practice with if. Keep in mind two important points: (i) In each part you are only allowed to use if, the names of the parameters, true, and false (though you may not need all of these); (ii) In the purpose statement of each, you must include the name of the common Boolean function you are implementing.

  • Design the function either-true? that takes two Boolean parameters and returns true if either (or both) of the parameters are true.

  • Design the function both-true? that takes two Boolean parameters and returns true if both of the parameters are true.

  • Design the function negate that takes one Boolean parameter and returns the opposite.

Exercise 4 Consider the following data definition (based on this information):
; A BrandiCarlileAlbum is one of ...
; - "Brandi Carlile (2005)"
; - "The Story (2007)"
; - "Give Up the Ghost (2009)"
; - "Bear Creek (2012)"
; - "The Firewatcher's Daughter (2015)"
; - "By the Way, I Forgive You (2018)"
; Interpretation: the name and year of release of Brandi Carlile's studio albums

  • Define three distinct examples of a BrandiCarlileAlbum.

  • Design the template for a function that takes in a BrandiCarlileAlbum.

  • Design the function next-album that takes in a BrandiCarlileAlbum and returns the next most recent BrandiCarlileAlbum (by year); if the function receives the most recent album it should return the oldest album.

  • Design the function prev-album that takes in a BrandiCarlileAlbum and returns the next oldest BrandiCarlileAlbum (by year); if the function receives the oldest album it should return the most recent album.

  • Design a World program that allows the user to press the arrow keys to "scroll" through Brandi Carlile albums: pressing left goes to the previous album (by year; or the most recent if currently displaying the oldest), right goes to the next album (by year; or the oldest album if currently displaying the most recent). The program should display the current album as black text. Recall that you will need to use (require 2htdp/universe) to require the big-bang library.

Exercise 5 Design the function multipurpose, which accepts a valid command and two strings. Valid commands are either former (which means to return the first of the two strings), latter (which means to return the second of the two strings), or ignore (which means to return the string "baz").

Exercise 6 Design a function countdown that will visually count down from some given natural number until it hits 0, at which point the program should stop and the screen should display "Done!".

Exercise 7 Design the function double which, given some string s, will display s, then ss, then ssss, then ssssssss, etc., in the center of the screen. It should stop after the text has gone past the edge of the screen.

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