This assignment is due on Wednesday, 2/21 at 11:59 PM. Submit it using the Handin server as assignment a7.
Important Note: Whenever we say to "design a function", we mean that you need to follow the design recipe. Any other time that you write a function in this class, you also need to follow the design recipe.
Exercise 1 Develop a data and structure definition for storing a Frequency, which combines a String and a Number and represents that many uses of that string. Call the structure frequency with fields word and count.
Exercise 2 Develop a data definition ListOfString that uses cons and empty to hold arbitrarily many Strings. Similarly, develop a data definition for ListOfFrequency that uses cons and empty to hold arbitrarily many Frequencys.
Exercise 3 Design a function count-word that consumes a ListOfFrequency and a String and adds 1 to the frequency for that string, producing a new ListOfFrequency. If there is no Frequency for that string, the resulting ListOfFrequency should have a Frequency with that string and the number 1.
Exercise 4 Design a function count-all-words that takes a ListOfString and produces a ListOfFrequency with the frequencies counted from the entire list of strings.
Exercise 5 Download the text of Hamlet into the same folder as your file where you are completing this assignment.
; (define hamlet-frequencies ...)
Exercise 6 Design a function frequents that consumes a ListOfFrequency and produces a ListOfFrequency that contains only the Frequencys from the original list where the number is more than 100. Use this to compute all the words used more than 100 times in Hamlet. Include this list in your submission, as another comment. (Did you know about the "Comment Out with Semicolons" command under the "Racket" menu in DrRacket? It’s handy.)
The Calder Foundation web site shows a variety of Calder mobiles.
Wikipedia describes a mobile as "a number of rods, from which weighted objects or further rods hang. The objects hanging from the rods balance each other, so that the rods remain more or less horizontal."
The Saint Louis Art Museum (free!) houses a nice Calder mobile in its collection.
; A Mobile is one of: ; - (make-leaf Number) ; - (make-rod Mobile Number Number Mobile) (define-struct leaf [weight]) (define-struct rod [lm ld rd rm])
Exercise 7 Design a function weight that takes a Mobile as input and computes its total weight. For simplicity, assume that the connection between the two sub-mobiles of a rod adds no weight.
Exercise 8 Design a function average-leaf-weight that takes a Mobile as input and computes its leaves’ average weight. You’ll need a helper function that also takes a Mobile as input.
Exercise 10 Design a function lighten that takes a Mobile as input and returns a new Mobile that is like the given one except everything weighs half as much as the given one. (So, your all-balanced? function should produce the same result for the given and returned mobiles.)
Exercise 11 Design a function enlarge that takes a Number and Mobile as inputs and returns a new Mobile that is like the given one except all the distances are that many times as long as the given one. (So, your weight, average-leaf-weight, and all-balanced? functions should produce the same results for the given and returned mobiles.)
Exercise 12 Design a function all-balanced-mobile that takes a positive integer as input and returns a Mobile that has exactly that many leaves and satisfies all-balanced?.
; counterbalance : Mobile -> Mobile ; adds a leaf to a given mobile in a balanced? way (define (counterbalance m) (make-rod m 20 20 (make-leaf (weight m))))
Extra fun 1 Design a function that draws a given Mobile.
Extra fun 2 Generalize rod so that each rod can hold not just two mobiles but any number of mobiles. Revise your functions to operate on your new data definition.