Homework 1

Assigned 13 Sept, due 20 Sept

This assignment is to be done in PLT Scheme/Racket. You should get at least some of these functions written in class on Sept. 13, and do the rest on your own time. You are encouraged to do this in teams of two, both students working together on each function; if you do this, turn in one homework with both names on it.

For all functions, *follow the design recipe*: each function
must have a contract, a good collection of test cases, and a function
definition.

This looks like a lot of programs to write, but most of them will each only be two or three lines long.

Write a function

`rect-perimeter`

which takes in the width and height of a rectangle and returns its perimeter.Write a function

`circle-perimeter`

which takes in the radius of a circle and returns its perimeter (aka circumference).Write a function

`Celsius->Kelvin`

which takes in a number of degrees Celsius and returns the corresponding temperature in Kelvin. (Kelvin degrees are the same size as Celsius degrees, but 0 Kelvin is approximately -273.15 Celsius.)Write a function

`Fahrenheit->Celsius`

which takes in a number of degrees Fahrenheit and returns the corresponding temperature in Celsius. (The formula is C = (F-32) * 5/9.)Write a function

`Fahrenheit->Kelvin`

which does what it sounds like.**Hint:**you should be able to write this function with no numbers or arithmetic operators in the function body, by re-using previously-written functions.Write a function

`convert-3-digits`

which takes in three numbers representing the hundreds, tens, and ones digits (respectively) of a three-digit number, and returns that three-digit number. For example, (convert-3-digits 5 0 4) should return the number 504.Write a function

`convert-3-reversed`

which takes in three numbers representing the ones, tens, and hundreds digits (respectively) of a three-digit number, and returns that three-digit number.**Hint:**you should be able to write this function with no numbers or arithmetic operators in the function body.The nation of Progressiva has a simple tax code. The tax you pay is your salary times the tax rate, and the tax rate is 0.5% per thousand dollars of salary. For example, if you make $40,000, your tax rate is 0.5% times 40, which is 20%, so you pay 20% of $40,000, which is $8,000.

Write a function to compute the net pay (i.e. pay after taxes) of a person with a given salary.

**Hint:**I recommend writing two helper functions as well as net-pay itself.Write a function

`teenage?`

that takes in a person's age in years and returns a Boolean telling whether the person is a teenager.Write a function

`any-two-equal?`

that takes in three numbers and tells whether any two of them are equal. (How many test cases do you need for this?)Write a function

`rough-age`

that takes in a person's age in years and returns one of the strings`"child"`

,`"teenager"`

, or`"adult"`

as appropriate.Write a function

`smallest-of-3`

that takes in three numbers and returns the smallest of them. (How many test cases do you need for this?)Write a function

`who-won`

that takes in three numbers: how many votes Anne got, how many votes Bob got, and how many votes Charlie got. It returns one of the four strings`"Anne"`

,`"Bob"`

,`"Charlie"`

, or`"tie"`

, depending on which person got the most votes. (The result should be`"tie"`

if two or more people tie for winner.) How many test cases do you need for this?Write a function

`above-diagonal?`

that takes in a`posn`

and tells whether it's above the y=x diagonal line. (Note: in computer graphics, positive x is to the right and positive y is*down*; the point (0,0) is at the top-left corner, not the bottom-left!)Write a function

`distance`

that takes in two`posn`

s and returns the straight-line distance between them (by the Pythagorean theorem).Write a function

`swap-x-y`

that takes in a`posn`

and returns a`posn`

whose x coordinate is the y coordinate of the parameter, and*vice versa*.Write a function

`scale-posn`

that takes in a number and a`posn`

and returns a`posn`

whose coordinates are the number times the corresponding coordinate of the parameter.Write a function

`add-posns`

that takes in two`posn`

s and returns a`posn`

whose x coordinate is the sum of the x coordinates of the parameters, and likewise for the y coordinates.Write a function

`reddish?`

that takes in a color and tells whether the red component is the largest of the three components.Write a function

`invert`

that takes in a color and two numbers (which we'll call*x*and*y*), and produces the negative of the given color by subtracting each color component from 255. Once you've tested this by itself, try calling`(map-image invert pic:bloch)`

Last modified: Stephen Bloch / bloch@adelphi.edu