Homework 1 is due Monday, October 2, 2017 at 11:59pm.
You will submit your work with filename hw1.rkt in a directory called hw1. The following commands, when run from within a copy of your repository, will get you started. Note the fourth instruction, touch hw1.rkt, will create a (blank) file of that name. Also, the command cd .. goes "up" one directory from the current working directory.
$ svn updateAfter having done this, work in the file hw1/hw1.rkt
$ svn mkdir hw1
$ cd hw1
$ touch hw1.rkt
$ svn add hw1.rkt
$ cd ..
$ svn commit hw1 -m "getting ready for homework 1"
At the moment this assignment is published, we have not yet taught you all you need to know to complete this work. Friday's lecture will fill in the gaps.
For the present, do not worry about functions being passed bogus inputs, e.g., a negative number of people. We will deal with erroneous arguments to functions later in the quarter.
Preliminaries
At the top of your hw1.rkt file, write this line to designate Typed Racket as the current language:
#lang typed/racket
To be able to use check-expect, etc., to test your functions, write this on the next line:
(require typed/test-engine/racket-tests)
On the line after that, type
(require "../include/cs151-core.rkt")This requirement includes a batch of definitions customized for this particular course. We will discuss cs151-core.rkt and its rationale later in the quarter.
At the bottom of the file, call the test function (with no arguments) to run the check tests:
(test)This function call — (test) — should remain as the last line of your program even as you are working on the code above it.
Homework Problems
For every function you write, you must preface the function definition with a type ascription (also known as a contract) and a purpose, and you must follow it with tests with check testers (check-expect, check-within, etc.). Do write helper functions where it seems like a good idea to do so. All functions need contracts, purposes and tests, even if they are "only" helper functions. (In actuality, a function is just a function, and the "helper function" designation is an illusion.) You may also call functions from other functions whenever you like.
Problem 1
Write the following functions:
- Write c->f and f->c to convert temperatures from degrees Celsius to degrees Fahrenheit, and vice versa. Both of these functions should have the type (Exact-Rational -> Exact-Rational) (alternatively written as (-> Exact-Rational Exact-Rational)).
- Write eval-quadratic to calculate the y-value of a quadratic function at a specified x-value. Specifically, it consumes exactly four arguments, a, b, c, and x. It evaluates to the y-value of the polynomial ax2 + bx + c at the specified x-value. The function should have type Real Real Real Real -> Real.
- Write the function distance of type Real Real Real Real -> Real. It must consume, in order, the x and y coordinate of one point, followed by the x and y coordinate of another point, and compute the distance between them. Use the usual formula for Euclidean distance.
Problem 2
Write the following functions that return true if and only if the corresponding condition is satisfied:
- low-battery? which takes a single argument, a battery percentage, and determines if the battery is low (defined as 20% or lower). Represent 20% by 0.2, 30% by 0.3, etc. The type of this function is Real -> Boolean.
- chicago-zip? must test whether the given Integer , which represents a zip code, is a Chicago zip code. For the purposes of this function, a Chicago zip code is any five digit number that begins with the digits 606. The function's type is Integer -> Boolean.
- circle-contains? which takes a circle's center's x and y coordinates, followed by its radius, followed by the x and y coordinates of a point. You may assume the given radius is positive. Determine if the point is (strictly) within the circle. The function's type is Real Real Real Real Real -> Boolean.
Problem 3
Write these two functions:
(: vector-add-x (Real Real Real Real -> Real)) (: vector-add-y (Real Real Real Real -> Real))Each one should consume the x and y components of two vectors, in order: an x, a y, an x, a y. The first function returns the x component of the result of adding the two vectors; the second returns the y component.
Problem 4
Although it is now technically fall, it still feels very much like summer. Time to make some lemonade for you and your friends.
Your lemonade recipe uses one third of a lemon for every adult and one quarter of a lemon for every child who will be drinking it. Of course, you can't buy a partial lemon at the store, so you'll need to round up.
Write the function lemons which takes in two parameters: the number of adults and the number of children who will be drinking your lemonade, and yields the number of whole lemons you need to buy to make the drink. Each of these values must be an Integer. In other words, the function's type must be Integer Integer -> Integer. The function should not tell you to buy more lemons than are strictly necessary; at worst, some fraction of a single lemon should be left over after you make your drink.
In your implementation, you might find the following built-in function to be helpful: exact-ceiling, which consumes a number and produces the nearest integer equal to or greater than it.
One store sells lemons in bags of five; you cannot purchase single lemons at this store. Write a second function, lemons-in-bags, that takes the number of adults and the number of children, and yields the number of lemons you need to purchase if you go to that store. In other words, this function should always return a multiple of five, the least such multiple that is sufficient to make the required amount of lemonade. Unlike for the previous function, you may have whole lemons left over after preparing your drink (but never five or more).
Problem 5
Suppose you are located at the seashore looking out over the ocean. How far away could you see an object floating on the water before it disappears below the horizon due to the curvature of the earth? This figure is called the offing in nautical parlance, and is part of the idiom in the offing.
The offing varies with the observer's height above ground level. You can see farther when on a higher floor of a building, an extraordinary distance from an airliner at cruising altitude on a clear day, and possibly an entire hemisphere from space.
In computing the offing, e will make the following simplifying assumptions:
- The earth is a perfect sphere.
- There is no atmospheric refraction allowing objects to be observed even though they are below the horizon when drawing a straight line.
- There is no fog, mist, haze, smoke, cloud, or any other phenomenon causing imperfect visibility.
- The earth is perfectly "flat," that is, the surface conforms exactly to the shape of a sphere, with no hills, mountains, or waves in the ocean.
- The objects you are attempting to spot in the distance are of infinitesimal height above the surface themselves.
- The height specified is the height of your eyes above the ground.
Consider these two different kinds of distances:
- Line of sight: The length of a straight line drawn from your eye to the farthest object you can see.
- Ground distance: The length of the shortest arc along the ground from the ground beneath your eyes (possibly, where your feet are, but adapted appropriately if you are within a building or aircraft) to the farthest object you can see with your eyes.
Write two functions, offing-line-of-sight/meters and offing-ground-distance/meters, which calculate the farthest object you can see for a given height above the surface of the observer, using the "line of sight" and "ground distance" interpretations of distance, respectively. Each should take one argument, a height in meters (a Real), and return a distance in meters as a Real. Each function should have type (Real -> Real).
The wisdom of the ancients is relevant to this problem: here is Proposition 18 from Book 3 of Euclid's elements:
If some straight line touches a circle, and some other straight line is joined from the center of the circle to the point of contact, then the straight line so joined will be perpendicular to the tangent.(See any version of Euclid's elements, including the following: https://farside.ph.utexas.edu/Books/Euclid/Elements.pdf.)
Having written the two functions above, write two more: offing-line-of-sight/miles and offing-ground-distance/miles. These functions must both be of type (Real -> Real) and must both consume meters and produce miles.
Submit Your Work
Submit your work by committing hw1/hw1.rkt to your CS151 subversion repository by Monday, October 2 at 11:59pm. All your work must be contained in that one file.
You should commit your work early and often. Intermediate commits — that is, commits made along the way to the final commit — are strongly encouraged; only your last commit before the deadline will be evaluated by your graders.
A note on style. Please make sure your lines of code are not more than 80 characters long. You can see the current character number at the bottom center of the DrRacket window. Use line breaks to clarify your code, and press TAB frequently within DrRacket to align the code correctly.