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 update
$ svn mkdir hw1
$ cd hw1
$ touch hw1.rkt
$ svn add hw1.rkt
$ cd ..
$ svn commit hw1 -m "getting ready for homework 1"
After having done this, work in the file hw1/hw1.rkt

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:

Problem 2

Write the following functions that return true if and only if the corresponding condition is satisfied:

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:

Consider these two different kinds of distances:

We will calculate both. In your calculations, assume the radius of the Earth is uniformly 6371000 m.

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.