CS 1101 - A-term 12

Homework 1 - Composing Functions

Due: Tuesday, August 28 at 5pm

Read the expectations on homework. You will be doing this homework individually.

Assignment Goals


The Assignment

Preliminaries and Vocabulary

  1. Start up DrRacket. If this is the first time you've brought up DrRacket, you will be asked to choose a language. From the Language menu, choose Choose Language. From "How to Design Programs", choose "Beginning Student", and click "OK". Now, in the definitions window, write a comment that includes your name and your username. From the File menu, choose Save Definitions As.... Name your file yourLastName-hw1, where yourLastName is your surname. Click the Run button.

    In the Interactions window, type in the number 7, then hit the Enter key. DrRacket gives you back the number 7. This is an example of an atomic expression. We can also write compound expressions, which consist of a left parenthesis, an operator, some more expressions (either atomic or compound), and a right parenthesis. Here are some examples, each conforming to the definition of "expression" by repeatedly applying the two definitions given above. Type each expression into the Interactions window and make sure you understand the results returned by DrRacket.

    7

    5

    (+ 7 5)

    (- (+ 7 5) 3)

    (* (- (+ 7 5) 3) 2)

    (In the third expression, the operator is +, and the two arguments required by the + operator are two numbers, 7 and 5.)

    In class on Thursday and Friday we played with some built-in functions in the image library. Add the following line to the Definitions window:

    (require 2htdp/image)
    
    Now click the Run button. The functions in the image library are now available to your program.

    From the Help menu, choose Help Desk. A browser window will open. Under the Teaching heading, choose How to Design Programs Teachpacks. Click on entry number 2.2 (image.ss). Here you will find a description of all the pre-defined functions (operators) in the image.ss library. Notice the ...search manuals... box at the top of the page. Type in image-width to find information about that operator. When the DrRacket Helpdesk provides the matches for image-width, choose the one from "2htdp/image".

    Here is an expression that will display a solid red circle of radius 25:

    (circle 25 "solid" "red")

    Notice that this conforms to the definition of a compound expression. (The operator is circle. The three arguments supplied to the circle operator are a number, 25, and two strings, "solid", and "red".)

    In the Interactions window, write a compound expression that will display two solid circles stacked on top of each other. Both circles should have a radius of 25. The circle on the top should be red, and the circle on the bottom should be yellow. (Hint: look at the definitions of the operators circle and above in the help desk).

    In the Interactions window, write a compound expression that will display three solid circles on top of each other, the top one being red, the middle one being yellow, and the bottom one being green. Use a radius of 25 for all three circles.

    As you answer each of the remaining problems, use comments in your file to clearly indicate the number of the problem you are solving.

  2. It's a pain to keep typing in the same stuff over and over. Move to the Definitions Window. Use the define operator to define a constant with the name TRAFFIC-LIGHT equal to the last image you created in the previous step (three solid circles...). Hit the Run button. In the Interactions window, type TRAFFIC-LIGHT.

  3. It's so dull always having traffic lights in the same three colors. Let's define a function that allows us to stack three solid circles (of radius 25) on top of each other, using any three colors we choose. When we call the function, we will provide the three colors we want as arguments to the function. When we define the function, we will need to reserve 3 empty places (3 parameters) that will hold the three arguments we provide when we call the function. Here are the contract and purpose for our function:
    ;; custom-traffic-light:  string string string -> image
    ;; consumes three strings representing the colors of the top, middle, and
    ;; bottom lights and produces an image of a traffic light in those colors
    
    
    Copy the contract and purpose into the definitions window. Now write the function definition for custom-traffic-light. (Hint: here is the first line of the function definition, with parameter names top, middle, and bottom:)
    (define (custom-traffic-light top middle bottom)
    

    Hit the Run button. If DrRacket reports any errors, try to figure out what's wrong, make corrections, and hit the Run button again. In the Interactions window, call the function custom-traffic-light to create an image of a traffic light. Call the function with arguments "chartreuse", "magenta", and "brown".

Composing Functions

Before continuing, read and make sure you understand Chapter 3, especially section 3.1.
    For all remaining problems, make sure you write a contract and purpose for each function.

  1. Develop a function called collision-speed. The function consumes the masses and velocities of two objects travelling along a track that are involved in a collision that results in the objects being stuck together. The function produces the velocity of the resulting object.

    Here is some information to help you. Since the objects are travelling toward each other, one of them will have a positive velocity and the other a negative velocity. Each object has a mass and a constant velocity. The product of the mass and velocity is the object's momentum. The collision of two objects combines the two masses and preserves the momentum, i.e. the momentum of the resulting object is the sum of the initial two momenta. Consequently, the velocity of the new object is the result of dividing the new momentum by the combined mass.

    You should develop this program by first developing 3 auxiliary functions. The first function, momentum, calculates an object's momentum given its mass and velocity. The second function, combined-momentum, calculates the combined momentum of two objects given each object's inital momentum. The third function, combined-mass, calculates the combined mass of two objects given their individual masses. Use your three auxiliary functions in your solution to collision-speed. Remember, each function should be documented with its own contract and purpose.

  2. Develop a function total-house-cost. The function consumes the selling price of a house and the amount of the down-payment, and produces the grand total paid by the buyer for the house.

    The following terms apply: the mortgage is financed for 30 years. The interest rate is 4%, and the interest is calculated using the formula for simple interest (rate * amount * time). Extra fees total $1000.

    The principal is the difference between the selling price of a house and the down-payment. Only the principal is used in the calculation of the interest. The total cost of the house is the selling price plus the interest paid plus the fees.

    You should develop auxiliary functions and define constants as necessary. Make sure each function you define is documented with a contract and a purpose.

  3. Develop a function called four-square that consumes an image and produces an image that consists of 4 copies of the original image, as shown in this example:

    if the original image looks like this: lambda

    then the produced image should look like this: 4-lambdas

    Use the DrRacket help desk to learn about the following functions (available in the 2htdp/image library) that you might consider using for this problem:

    You may use the image in this problem for testing purposes, or you may test your program with an image of your own choosing (see Google images. To copy an image, right-click on the image, select Copy, then in the DrRacket Definitions window, select Paste. You should give your image a name using define.


Grading

These are the general grading guidelines for all assignments in this course. Here is the specific grading rubric the graders will use for Homework 1.

What to Turn In

Using web-based turnin (see note below), turn in a single file containing all code and documentation for this assignment. Name your file

yourLastName-hw1

For example, if your name is John Doe, you would name your file doe-hw1. In addition, your name and your wpi ccc username must be listed in a comment at the top of your file. Programs will not be accepted for submission after 5pm on Tuesday, August 28.

A turnin account will be created for each student in the class on Monday, August 27. On that day, you will receive an email from turnin containing your initial password. The email will be sent to your wpi email address.