7.4

Week 4 Set a

home work!

Programming Language BSL

Due Date Monday 9/23 at 9:00pm (Week 4)

Purpose To practice designing programs and begin working with self-referential data.

Finger Exercises

Exercise 1 From HtDP, 123 128

Graded Exercises

Guess My Number

Exercise 2 Design a program which, when launched, randomly generates a number n from 0 to 9. On display should be the word "Guess!". Then, once a person guesses a number, it should display that number as well as either "Nope, higher.", "Nope, lower.", or "You've won!" based on whether n is higher, lower, or equal to the number guessed. The program should end when the correct guess is made and output the number of guesses it took to find the correct number.

Be sure to define your data and follow the design recipe all the way through!

Note: when a function input is ignored, it is called _ by convention. Also, its type in the signature is Any, since we accept any kind of data. Any will be formalized later on in the semester; don’t worry about defining it for now.

Hint: Sometimes, when building a program, it is easier to build a slightly simpler program that is easier to debug. One may want to initially design the program so that the number to be guessed is given to the function as opposed to being randomly generated. Then, once they are sure this version of the program works, modify it to randomly generate the number to be guessed.

What’s in That Salad?

; A Salad is one of:
; - "lettuce"
; - (make-ingredient String Salad)
(define-struct ingredient [name more])
; and represents a collection of ingredients in a salad

Exercise 3 Define examples of and design the template for a Salad.

Exercise 4 Design a function which describes a salad. A salad with just lettuce should be described as "This salad has lettuce". A salad with walnuts (as the outermost ingredient) and feta should be described as "This salad has walnuts and feta and lettuce".

The Tower Of Terror

; A Building is one of:
; - "ground"
; - (make-story Number PosInt String Building)
(define-struct story [height rooms color below])
; and represents either the ground story,
; or a story with a height, number of rooms, color, and
;   the rest of the building beneath it

Exercise 5 Define examples of and design the template for a Building.

Exercise 6 Design a function which counts the total number of rooms in a Building.

Exercise 7 Design a function which draws a building. The stories should be of a fixed width, be their own color, and stacked on top of each other. Each story should also consist of horizontally arranged, identically sized rooms, all of which are surrounded by a frame to differentiate between them. The ground story can be represented by the empty-image.

You may find the following data definition and template helpful, and keep in mind all positive integers are instances of natural numbers:

; A Nat is one of:
; - 0
; - (add1 Nat)
 
; nat-temp : Nat -> ?
(define (nat-temp n)
  (cond [(zero? n) ...]
        [(positive? n) (... (nat-temp (sub1 n)))]))