Problem Set 6

home work!

Programming Language BSL with List Abbreviations

Purpose This problem set (primarily) concerns the design of functions on self-referential data definitions.

You must follow the design recipe. The graders will look for data definitions, contracts, purpose statements, examples/tests, and properly organized function definitions. For the latter, you must design templates. You do not need to include the templates with your homework, however. If you do, comment them out.

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

Finger Exercises HtDP/2e: 166, 167, 168, 169, 171, 172, 173, 174, 175, 176; (As usual, "finger exercises" will not be graded; they are just for you to practice on.)

Due Tuesday, February 24 at 7pm image

Problem 1

You are designing software for the post office to help process physical mail. There are two kinds of physical mail: letters and boxes. A letter has an address (which we’ll represent simply, as a string) and a weight (measured in ounces). A box has height, width and length dimensions (measured in inches) and a weight (measured in ounces).

There are some rules for mail:
  • A letter must weigh less than 3.5 ounces.

  • The sum of a box’s height, width and length must be 62 inches or less, its total volume must be 7938 cubic inches or less, and its weight must be 50 pounds (800 ounces) or less.

  • A letter costs fifty cents to mail, while a box is fifteen cents an ounce.

Here are the tasks before you:
  1. Design a data definition, Item, to represent one item of mail.

  2. Design a function, item-ok?, to determine if a piece of mail satisfies the rules.

  3. Design a data definition, LOI, for a list of items.

  4. Design a function, bad-items, that takes a collection of mail (represented as an LOI), and returns all the items that don’t satisfy the rules.

  5. Design a function, total-postage, that produces the total postage for a collection of mail.

Problem 2

Using Unicode characters, we can get a very close approximation of upside-down text for lowercase letters. The lowercase letters have been encoded into Unicode, creating a full set of upside-down lowercase letters (more or less). (Try typing in "\u0250" in the interactions window in DrRacket.)

Design a program that will consume a string of lower-case letters and produce the string upside-down. Create helper functions as needed.

A few helpful hints to get you started:

You can use explode to produce a list of 1-letter strings from a string:

;;explode: String -> List-of-String

;;translates a string into a list of 1-letter strings

;;example: (explode "cat") -> (list "c" "a" "t")

And implode will allow you to produce a string from a list of strings:

;;implode: List-of-String -> String

;;concatenates a list of 1-letter strings into one string

;;example: (implode (list "c" "a" "t")) -> "cat"

We don’t want you to labor over the Unicode entry, so a table is provided below:

;; A LOS is one of

;; - empty

;; - (cons String LOS)

;;

;; A Table is one of

;; - empty

;; - (cons (cons String (cons String empty)) Table)

 

;; build-table: LOS LOS -> Table

;; build a table from two lists of strings

;; assume: lists are equally long

(define (build-table domain range)

  (cond

    [(empty? domain) empty]

    [else (cons (list (first domain) (first range))

                (build-table (rest domain) (rest range)))]))

 

(check-expect (build-table (list "a") (list "b"))

              (list (list "a" "b")))

 

(define table-1

  (build-table

   (explode "abcdefghijklmnopqrstuvwxyz?!’")

   (explode "ɐqɔpǝɟƃɥıɾʞןɯuodbɹsʇnʌʍxʎz¿¡,")))