Problem Set 4

You must follow the design recipe. However, when you turn in your homework make sure you comment out your templates.

You might also note that your graders are now being instructed to grade off for gruesomely laid-out code; proceed accordingly.

Submit early and often.

This assignment will be due on Friday night, instead of Thursday night.


home work!

Programming Language BSL

Purpose This problem set is a first study of making design choices. It shows that the very first design choice concerns the data representation. Making the right choice requires planning ahead and weighing the complexity of each. Of course, getting good at this is a question of gathering experience.

Finger Exercises HtDP/2e: 120, 124, 125, 126, 127, 133. The finger exercises this week are independent of the problems below.


Problem 1 Fanciful Fish

The aquarium has decided to host a special gala, of color-coordinated displays of fish. In the past they’ve noticed that green fish are the most popular kind of fish at the aquarium. The aquarium has decided that the best possible way to improve visitor attendance is to corral all the green fish into the centerpiece tank in the aquarium.

A fish is defined as follows:
; A Fish is a (make-fish String Color PositiveNumber)
; - where the String is the fish's name
; - the Color is the color of the fish
; - and the PositiveNumber is the weight of the fish
(define-struct fish (name color weight))
And a tank is defined as follows:
; A Tank is one of:
; - (make-empty-tank)
; - (make-tank Fish Tank)
(define-struct empty-tank ())
(define-struct tank (first-fish other-fish))
  • Design a function only-green-fish that takes a Tank and returns a Tank containing only the green fish.

    Additionally, design the function non-green-fish that takes a Tank and returns the Tank containing the fish that are left behind.

  • Given the incredible success of their green fish tank, and the surprising failure of their side-show of purple fish, the aquarium has decided that the best way to improve visitor attendance further is to paint all the purple fish green.

    Design a function paint-purple-fish that takes a Tank and returns the same Tank but with all the purple fish colored green.

  • Fashion is fickle, and people seem to be bored with the endless parade of green fish. The new favorite seems to be pink fish. The aquarium has realized that people’s tastes will keep changing forever. Unless there are no fish. Or no people. But then it would be a sad aquarium. Anyway, they have decided to generalize.

  • Design a function go-fish that takes a Tank and a Color and returns a Tank containing only the fish of the given color.

  • Design a function paint-fish that takes a Tank and two Colors, original and new-color, and returns the same Tank but with all the original colored fish changed to new-color.

  • Design the functions only-green-fish-v2 and paint-purple-fish-v2 which do the same things as the functions you wrote before but utilize your brand new go-fish and paint-fish functions. This second version should be very short. Testing version 2 against version 1 is a good way to ensure correctness.


Problem 2 Look who’s talking

A Paragraph is a List of Sentence. A Sentence is a List of Word. A Word is simply a string with no spaces, and no punctuation marks.

Make sure you design all your code carefully – including testing it well!


Problem 3 A picture’s worth a thousand words

Now that you’ve got paragraphs of sentences, you want to show them off to the world. But you measure your words carefully: instead of blurting them out all at once, you meter your sentences one at a time...

Define a world as
; A TextWorld is a (make-world Image Paragraph)
; - where the Image shows the sentences revealed so far, and
; - the Paragraph are the sentences yet to be shown.
(define-struct world (image hidden))

Define a world program that produces a slide show of your sentences: every second, it reveals one more sentence underneath the previous ones, on a 400x400 scene:

image image image

You will need to design the functions display that renders a world, and next that produces the next world to be rendered. You may be interested in using the above/align function. Kick off your program with
(big-bang WORLD0
          (on-tick next 1)
          (to-draw display))