CS 1101 - A-term 10

Homework 1 - Composing Functions

Due: Tuesday, Aug 31 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 (see "What to Turn In", below).

    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 htdp/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 Languages heading, choose Teachpacks. Click on entry number 1.1 (image.ss). Here you will find a description of all the pre-defined functions (operators) in the image.ss library. Notice the search 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 "htdp/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 overlay/xy 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:
    ;; 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 traffic-light. (Hint: here is the first line of the function definition, with parameter names top, middle, and bottom:)
    (define (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 traffic-light to create an image of a traffic light. Call the function with arguments "chartreuse", "magenta", and "brown".

Composing Functions

  1. Download this DrRacket file for HW1 onto your computer. In DrRacket, open a new tab by choosing "New Tab" from the File menu. In the new tab, choose "Open" from the File menu to open the file you just downloaded. Copy and paste the contents of this new file into the Definitions window of your homework file. Your program now contains images of several different kinds of foods.

    Define constants that represent the price of each food item, according to this price list:

    hot dog   2.95
    taco      1.50
    fries     1.50
    burger    3.95
    salad     5.00
    pizza    12.00
    

  2. You and your friends decide to order a pizza and share the cost according to how many slices each person eats. The pizza is cut into 8 slices. Write a function called share-pizza-cost that consumes the number of slices eaten by one person and determines that person's share of the cost of a pizza. For this and all remaining problems, make sure you write a contract and purpose for each function.

  3. Define a function called price-with-tip that will add a 15% tip to a given number. (Write a contract and a purpose for your function.)

  4. Define a function called share-with-tip that consumes the number of slices of pizza eaten by one person and computes that person's share of the cost of a pizza, including the cost of the tip. Use the functions you defined in Problems 4 and 5.

  5. Write a function add-side that consumes two images (for a meal item and a side-dish item), and produces an image similar to this, picturing both the meal item and the side item:


    Don't forget to document the function with a contract and purpose.

  6. A waiter working at the pizza shop can stack two items in his arm when he delivers an order to a table. Of course, he knows how to balance the items so they don't fall over. Write a function stack-two that consumes images of two items and returns an image with the second item just above the first one. To ensure that the stack of items is balanced, you should center the items one above the other, like this:

    The images must be centered regardless of their sizes (in other words, don't assume that the images supplied as inputs to the function are of any particular height or width).

    [HINT: It's difficult to know if your function is working correctly if you test your function only with the images supplied in this assignment, since they are all of similar width and height. I suggest testing your function by stacking (and centering) two images of your own creation of obviously different dimensions.]

    Note that DrRacket has a function max that consumes two numbers and produces the larger of the numbers. You should also be using the image-width and image-height functions provided with the image.ss teachpack.

    Is your function getting too complicated? Should you decompose the problem into multiple functions?


Grading

Here is the grading rubric that 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 Jane 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 31.

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