Homework 5

home work!

Programming Language : BSL

Due Date: Thursday February 13, 9pm. This is Exam Day! For this reason, this homework is a little shorter than usual.

Purpose: to design simple list processing functions

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

Exercise 1 Recall the eclipse program presented in Lecture 12 (Lecture Notes 02-03.txt). This program increases the number of moons by 1 when a key is pressed. It allows an arbitrary number of moons, but it is still pretty simplistic: the only change applicable to the number of moons is to increment it by 1.

Show that we really have a lot of freedom to change the number of moons, up or down, by 1, 2, 3, ... moons at once, as follows. Begin by copying the eclipse program from the lecture into your Racket file and make sure that it works. In this problem you will modify the on-key clause of big-bang. Pretty much all else stays the same in this program.

  • Define three new moons (NEW-MOON-1, NEW-MOON-2, and NEW-MOON-3), with pairwise different x-coordinates and speeds +1, –1, and +1, in this order. Note that a positive speed means to move to the right, while a negative speed means to move to the left. You can pick different absolute speed values if you like, but make the signs alternate, as done above.

  • Replace the on-key handler function (called add-moon in the lecture) by a new function called change-moon-count, which implements the following key-event handling:
    • If key "2" is pressed, the number of moons increases by 2, by adding moons NEW-MOON-1 and NEW-MOON-2 to the system, in this order.

    • If key "3" is pressed, moons NEW-MOON-1, NEW-MOON-2, and NEW-MOON-3 are added to the system, in this order.

    • If one of the keys backspace or delete is pressed, the number of moons decreases by one, by dropping the first moon in the system. (This is the moon that was added to the system most recently and has not yet been removed.) If there are no moons in the current system, these key-press events have no effect. The key-press events are represented as the strings "\b" and "\u007F", resp.

    • If any other key is pressed, the number of moons increases by 1, by adding moon NEW-MOON-1.

Exercise 2 Revisit the ListOfString data design from one of the recent lectures and copy it into your file. Then design a function are-there-two? that takes a String and a ListOfString and returns true whenever the given list contains (at least) two copies of s.

Hint: you will need a helper function.

Exercise 3 The following is the design of simple data to represent a city:
(define-struct city [name country pop])
; A City is a (make-city String String Nat)
; repr. the name, country, and population of the city
(define CITY-BOS (make-city "Boston"     "USA"           700000))
(define CITY-GEN (make-city "Geneva"     "Switzerland"   450000))
(define CITY-ZUR (make-city "Zurich"     "Switzerland"   450000))
(define CITY-TOK (make-city "Tokyo"      "Japan"       10000000))
(define CITY-LUC (make-city "Luckenbach" "USA"               12))
(define CITY-0   (make-city "Zero"       "Zero"               0))

  • Design a function city-max-2 that takes two Citys and returns the one with larger population. Resolve the case of equal population either way.

  • Design data ListOfCity (LoC) to represent a list of cities.

  • Design a function city-max that takes a LoC and returns the largest city, in terms of population. If the given list is empty, return CITY-0. If there are several largest cities, return any one of them.

  • Now suppose the cartographer who tasked you with writing this function is particular about ties and always wants the first among the largest cities in the list to be reported. Consider:

    (check-expect (city-max (list CITY-LUC CITY-GEN CITY-ZUR)) CITY-GEN)

    Explain how your code must be changed to satisfy this requirement. If nothing needs to be changed, explain why your code satisfies this requirement.

    Hint: this may effect both the city-max-2 and city-max functions.