# CSG 107: Problem Set 4

Due: Wednesday, February 11, 2009 at 500 pm.

#### Purpose:

The goal of this problem set is to help you design abstractions and to deploy pre-defined abstractions in a functional context. In addition, the assignment should familiarize you with closures.

Remember that you must follow the design recipe. Your deliverables include the data analysis, contract and purpose header, design strategy and template, code, and tests.

#### Drill:

HtDP: 19.1.6, 20.2.2, 20.2.4, 21.2.2, 21.2.3

#### Required Problems:

Note 1: You must use DrScheme's HtDP Intermediate Student Language with Lambda.

1. HtDP, problem 21.1.2
2. You are the President of the United States and you need to evaluate several schemes for bailing out the banks.
```(define-struct bank (name total-assets bad-assets old-price new-price))
;; A Bank is
;; (make-bank String Number Number Number Number)
;; where
;; -- name is the name of the bank
;; -- total-assets is the total assets of the bank
;; -- bad-assets is the amount of bad assets of the bank
;; -- old-price is the price of the bank's stock on January 1, 2008
;; -- new-price is the price of the bank's stock on January 1, 2009

(define-struct bailout (name amount))
;; A Bailout is (make-bailout String Number)
;; where
;; -- name is the name of the bank
;; -- amount is the amount the government gives the bank
```

You are considering two formulas:

1. One is to give each bank a percentage of its bad assets
2. The second is to give each bank an amount that is a percentage of the product of its total assets times its decline in stock price from 1/1/08 to 1/1/09. In no event is the bank to be paid a negative amount.

Design the functions:

```bailout1 : Number (listof Bank) -> (listof Bailout)
bailout2 : Number (listof Bank) -> (listof Bailout)
total-cost1 : Number (listof Bank) -> Number
total-cost2 : Number (listof Bank) -> Number
```

These functions should compute the amount to be paid to each bank, and the total cost for each formula. In each case, the Number argument is the percentage to be used in the formula.

After hearing your results, the President decides that these programs cost too much, and the percentages need to be scaled back. Design functions

```magic-bailout-percentage1 : Number (listof Bank) -> Number
magic-bailout-percentage2 : Number (listof Bank) -> Number
distribute-bailout1 : Number (listof Bank) -> (listof Bailout)
distribute-bailout2 : Number (listof Bank) -> (listof Bailout)
```
In each case, the Number argument is the total cost of the bailout. (magic-bailout-percentageX c lob), should return the percentage required so that the total bailout cost for lob matches the given cost c.

Stating this algebraically, we want, for any number c and list of banks lob,

```(total-cost1 (magic-bailout-percentage1 c lob) lob) = c
(total-cost2 (magic-bailout-percentage2 c lob) lob) = c
```
The distribute-bailout functions produce the (listof Bailout) corresponding to the calculated percentage.

3. The president decides that neither of these formulas is any good, and tells his Cabinet to go home and make up a few dozen more formulas to try out. Rewrite your solution to the preceding problem in a way that will allow the Cabinet to use your functions for any given formula, so long as it is formula expressed as a function that satisfies the contract Bank -> Number.
4. Modify your solution to the Matryoshka doll problem from the preceding problem set as follows:

1. Instead of having 9 numbered positions on the stage, you may have any number of dolls positioned anywhere on the stage.
2. Instead of using keyboard input, you drag and drop with the mouse. Pick up the top layer of a doll by clicking anywhere within its rectangular image. Drop a doll layer by releasing the mouse button anywhere over the vertical projection of an existing doll on the stage. If you release the mouse button in a position that is not over an existing doll, then the layer drops to the stage by itself, and can have other layers dropped on it, etc. As bfore, if the layer you are dropping is too small to fit where you are dropping it (either because it is doll<? the doll you are dropping it on, or because it won't fit between two adjacent dolls), you should reject the move, as you did in Problem Set 3.