% Homework: Simple functions and conditionals % COMP 50 % Fall 2013 This homework is due at **11:59PM on Monday, September 16**. If all goes well, you will download an extension to DrRacket that enables you to submit your homework direct from DrRacket. If not all goes well, Plan\ B will be announced on Piazza. All the exercises should be done using the *Beginning Student Language* on the "choose language" option DrRacket's Language menu. You can use the same menu to add teachpacks; when you do, the display in the lower left should read `Beginning Student custom`. Finger Exercises ---------------- For this homework, I am recommending the following finger exercises: - Exercises 2.3.1 and 2.3.2 in the book. - Write function `years->months` which converts a number of years to a number of months. - Write function `radians->degrees` which converts an angle in radians to an angle in degrees. - Write a function that takes an image and returns the string "landscape" if the image is wider than it is tall, "portrait" if the image is taller than it is wide, and "square" if the image is as equally tall as it is wide. You will need to add the `image.rkt` teachpack from the `2htdp` teachpacks. - Write a function that is given the value of $\cos\ \theta$ and that returns the great-circle distance in *meters* between two points on the Earth's surface that are separated by the angle $\theta$. **Domain knowledge:** According to the official "geoid datum", the Earth's radius is deemed to be 6378137.0 meters. The Earth is official not a sphere, but you should treat it as such. **Racket knowledge:** using the Racket Documentation button on the Help menu, search for and understand the function `acos`. Problems to submit ------------------ 1. Define function `dd/mm/ss->radians` which converts an angle in degree-minute-second form ($DD^\circ$ $MM$" $SS.SSS$') to radians. 2. Since the time of the ancient Greeks, people have used geographic latitude as an indicator of climate. The old latitude models have been superseded by more sophisticated models that take account of terrain, winds, and ocean currents, but they are still not a bad approximation. Using the following table, define a function that converts a latitude in degrees to the name of a climate: Latitude range Expected climate ----------------- ------------------- below 23.5 tropical 23.5 to 35 subtropical 35 to 50 temperate 50 to 70 subpolar above 70 polar 3. Century Bank in Medford offers certificates of deposit at the following interest rates: Term Rate ------------------------------------ --------- 3 month up to 6 months 0.25% 6 months up to 1 year 0.35% 1 year to 2 years 0.50% 2 years to 3 years 0.80% 3 years to 5 years 0.90% 5 years 1.00% 36 months (*for new customers only*) 1.34% 48 months (*for new customers only*) 1.83% 60 months (*for new customers only*)\ \ \ 2.03% Define two functions, one for regular customers and one for new customers, each of which takes a given term in months and returns the interest rate available for a certificate of deposit with that term. 4. Using teachpack `guess.rkt`, available from the Language menu (Add Teachpack), do Exercise 5.1.2 in the textbook. 5. [A Labrador retriever will eat anything](http://www.youtube.com/watch?v=r14DJ7sUP_U). And if a piece of orange chicken is dropped on the floor, within the first 100 milliseconds, the Lab will close 10% of the distance between itself and the chicken. Using the `2htdp/image` teachpack, design a *program* that illustrates the first step taken by the Labrador retriever: - The retriever and the chicken should be located in an empty room that measures 300 by 300 pixels. - The main function should take two `posn` values called `dog` and `food`. It should produce an image in which - The dog is represented by a solid black circle. - The chicken (`food`) is represented by an orange dot. - The expected trajectory of the retriever is shown as a green line. - The location where the retriever will be in 100 milliseconds, which is 10% of the distance along its trajectory, is shown as an outlined black circle. Here is a sample output: ![Sample retriever step](retriever.png) **Domain Knowledge (Geometry)**: To find the black circle between the dog and the food, compute 10% of the *signed* distance in each dimension and add it to the coordinates of the dog. To be sure you get the correct sign of the distance, you'll want to consider several configurations. **Domain Knowledge (Computer Graphics)**: By convention, in computer graphics, larger values on the $y$ axis are *down*. **Domain Knowledge (Dogs)**: the shortest distance between a dog and food is a straight line. Follow the design recipe for programs in Section 3 of the textbook. You may find it helpful to consult Section 3 of the [second edition](http://www.ccs.neu.edu/home/matthias/HtDP2e/part_one.html#%28part._ch~3ahtdp%29), especially [Section 3.4 (From Functions to Programs)](http://www.ccs.neu.edu/home/matthias/HtDP2e/part_one.html#%28part._sec~3adesign%29). Karma problems -------------- A. Change the traffic-light animation from the lecture (which is very similar to an example from the second edition) so that the relative times for red, green, and yellow lights behave more like real traffic lights. You will need to use the `big-bang` function in the `2htdp/universe` teachpack. You will also need to define a *world state* that knows not only what color the light is but also how much time is left before the light is due to change. B. Using the `big-bang` function in the `2htdp/universe` teachpack, animate the Labrador retriever going for the food. Have the retriever cover 10% of the remaining distance at each step. ^[As the dog closes in on the food, the dog slows down. This is not how real dogs behave.] When the dog closes to a distance within 5 pixels of the food, make the food disappear and stop the animation. Resources --------- For information on the `2htdp/image` and `2htdp/universe` teachpacks, you can go directly to the Racket Documentation. (We will also do some image things in lab.) For a more leisurely introduction, - [Section 1.4 of the second edition](http://www.ccs.neu.edu/home/matthias/HtDP2e/part_one.html#%28part._sec~3aarith-images%29) give a thorough introduction to images. - [Section 3 of the second edition](http://www.ccs.neu.edu/home/matthias/HtDP2e/part_one.html#%28part._ch~3ahtdp%29) is primarily about designing programs as collections of functions, but it has a great many examples of `big-bang`. You can develop examples any way you want, but **your submitted examples must be formulated as test cases.** Use `check-expect` or `check-within`. Put your tests in with your definitions. How your work will be evaluated ------------------------------- We will evaluate your work by judging it against the table of criteria below. The table may look big and intimidating, but it boils down to these issues: - Did you use Beginning Student Language with suitable teachpacks? - Is the code well formed? That is, is it laid out nicely on the page? - Are the names well chosen? - Have you followed the design recipes? 1. Are there data descriptions where needed? For conditional functions, does the code show that all possible situations have been enumerated? 2. Does every function have an *appropriate* signature (called "contract" in the first edition), purpose statement, and header? 3. Did you come up with examples? 4. Has the function body been developed by following a template that is suited to the problem and to the class of data being consumed? 5. In the coding step, have you carried out the purpose statement and only the one purpose statement? 6. Does your submission included test cases derived from your examples using `check-expect` and/or `check-within`? Are the test cases sufficient to handle every situation from your analysis and every clause in every relevant data description? Especially as you get started, I urge you to get a member of the course staff to examine your work *before* the deadline, to make sure you know how to follow the design recipe. You can come to office hours or post *private* questions on Piazza. % rubric