CSC 270
Homework 1
Assigned 8 Sept, due 15 Sept

This assignment is to be done in PLT Scheme. You should get at least some of these functions written in class on Sept. 8, 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.

    Function definition

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

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

  3. 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.)

  4. 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.)

  5. 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.

  6. 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.

  7. 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.

  8. 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.

  9. Booleans

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

  11. 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?)

  12. Conditionals

  13. 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.

  14. 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?)

  15. 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?

  16. Posns (built-in structure type)

  17. 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!)

  18. Write a function distance that takes in two posns and returns the straight-line distance between them (by the Pythagorean theorem).

  19. 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.

  20. 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.

  21. Write a function add-posns that takes in two posns and returns a posn whose x coordinate is the sum of the x coordinates of the parameters, and likewise for the y coordinates.

  22. User-defined structs

  23. Using the definition of person from class, write a function older? that takes in two persons and tells whether the first is older than the second.

  24. Using the definition of candidate from class,
    (define-struct candidate (name votes)))
    write a function who-won that takes in three candidates and returns the name of the winner, or "tie" if there's a tie for first place.

  25. Write a struct definition to represent a time of day, with hours, minutes, and seconds. (Assume a 24-hour clock.)

  26. Write a function secs-since-midnight that takes in one of your time-of-day objects and returns the number of seconds since the most recent midnight.

  27. Write a function add-a-vote which takes in a candidate and returns one just like it, but with one more vote.

  28. Write a function add-a-second which takes in a time-of-day object and returns another one a second later. (Note that the number of seconds should not exceed 59, the number of minutes should not exceed 59, and the number of hours should not exceed 23.)


  29. Last modified: 
    Stephen Bloch / bloch@adelphi.edu