6.6

Homework 4

home work!

Programming Language BSL

Due Date Thursday February 6, 9pm

Purpose To practice using union data and self-referential data.

Failure to comply with these expectations will result in deductions and possibly a 0 score.

Exercise 1 We live in a one-dimensional world called The Line. The location of all living creatures can be specified by a single coordinate.

  • Design data Bug to represent a bug, which has a location and a body temperature in degrees Fahrenheit. (You can assume that any number is a valid Fahrenheit temperature; no data design required.)

  • Design data Bird to represent a bird, which has a name, a location, and information as to whether the bird can sing.

  • Design data Animal to represent an animal, which is either a bug, a bird, or Nessie (the Loch Ness monster – this is a unique animal, and of course it exists).

    Also, using the Animal template, define a recognizer predicate animal? that takes an arbitrary type of data as input (All) and returns true exactly if the given data is an Animal. (Why do we actually need to write such a function–aren’t these automatically provided by DrRacket?)

    If, after using the template, you feel like simplifying the function, you may do so, but provide the "template version" of your code first.

  • Design data TwoAnimals to represent two animals.

  • Design a function danger? that takes a TwoAnimals and returns true exactly if the TwoAnimals contains a bug and a bird AND the two are less than 10 pixels apart from each other (in which case the bird will snatch the bug).

    Make sure to design enough tests to cover all possible combinations of Bug and Bird in a TwoAnimals.

  • Design a function metamorph that takes a TwoAnimals and returns an animal OR a TwoAnimals, as follows. If the TwoAnimals contains two colliding bugs (same location on The Line), both with a body temperature of at least 80 degrees Fahrenheit, they need to cool off and change into the Loch Ness monster. In all other cases, no metamorphosis happens: return the original TwoAnimals.

    You will likely need to design additional data. Remember to use helper functions for all separate chunks of work, and design them top-down.

Exercise 2 The following code is an excerpt of the code we wrote for the CollisionSim example. "Excerpt" means that, for brevity, we show here only the part that is required by DrRacket to run it. We omit data definitions, since this is all included in the lecture notes posted on Piazza. Specifically, you may wish to review the postings 01-23.txt and 01-27.txt (although they are not required to do the following exercise).

(define-struct twocars [x1 v1 x2 v2])
 
(define TWOCARS-1 (make-twocars 1 2 600 4))
(define TWOCARS-2 (make-twocars 300 10 300 20))
 
(define-struct wreck (x v))
 
(define WRECK-1 (make-wreck 300 -10))
 
(define CS-1 TWOCARS-1)
(define CS-2 TWOCARS-2)
(define CS-3 WRECK-1)
 
(define (move-wreck w)
  (make-wreck (+ (wreck-x w) (wreck-v w))
              (wreck-v w)))
 
(define (collided? tc)
  (>= (twocars-x1 tc) (twocars-x2 tc)))

In the context of the above definitions, consider now the following code:
:  1 ; move-CS : CS -> CS
:  2 ; move-CS takes a CollisionSim and returns a new CollisionSim
:  3 (check-expect (move-CS CS-1) (move-twocars CS-1))
:  4 (check-expect (move-CS CS-3) (move-wreck   CS-3))
:  5 (define (move-CS cs)
:  6   (if (twocars? cs)
:  7       (move-twocars cs)
:  8       (move-wreck cs)))
:  9
: 10 ; move-twocars : Twocars -> Twocars
: 11 ; move the car during 1 clock tick
: 12 (define (move-twocars tc)
: 13   (if (collided? tc)
: 14       (make-wreck (/ (+ (twocars-x1 tc) (twocars-x2 tc)) 2)
: 15         (- (twocars-v1 tc) (twocars-v2 tc)))
: 16       (make-twocars (+ (twocars-x1 tc) (twocars-v1 tc)) (+ (twocars-v1 tc) 0.1) (- (twocars-x2 tc) (twocars-v2 tc)) (+ (twocars-v2 tc) 0.1))))

This code "works"! That is, all check-expects pass. But it is still bad: there are numerous errors, style violations, etc.

Your task in this exercise is to perform a partial code review. That is, imagine you are part of a small team of 4–5 programmers (who program individually or in pairs). It is the policy of your company that occasionally each programmer displays the code they wrote for the past week and discusses its design. The other 3–4 programmers listen and then critique the code, and ask questions about it. You will soon see a simulation of a code review in the Lab.

For this homework, we only do the "critique" part: look at the code shown in lines 1–16 above (not the definitions at the beginning of this exercise), and identify all problems you can find. For each,
  • indicate the line of the problem, and the location within the line if necessary,

  • describe the problem: is it a style violation? is the signature wrong? an issue with the check-expects? does the code have a semantic error, i.e. one that DrRacket may not report, but that may still cause wrong results to be computed?

  • if applicable, provide an improvement/correction/solution.

Report on your findings in comments in your Racket file. You should be able to find up to 10 violations.

Exercise 3 Go back to our one-dimensional zoo of bugs, birds, and Nessie. Dr. Richard Byrd, the famous ornithologist, says: "Who cares about bugs? Write me a function that extracts all birds from a zoo and returns their names separated by ", " as a line of text, like "warbler, mockingbird, falcon".

Fulfill Dr. Byrd’s wish, as follows.
  • Design data Zoo to represent an arbitrary number of animals.

  • Design a function the-birds that takes a Zoo and does as Dr. Byrd wants. How can you ensure that there is no embarrassing trailing or leading comma in the output? That is, avoid returning output of the form: "mybird, yourbird, "