CS 1101 - Aterm 07

Homework 1 - Warming up in Scheme

Due: Sunday, August 26 at 11:59pm

This homework is to be done individually. Do not work with a homework partner on this assignment.

Read the expectations on homework.


Assignment Goals


The Assignment

What do we mean when we say "write an expression" or "define a constant"? What is an operator? Is an operator the same as a function? What's the difference between "defining a function" and "calling a function"? What is a parameter? What is an argument? After doing this homework, you should know the answers to these questions.

You will be turning in the solutions to problems 6, 7, 9, and 10 only.

  1. Start up DrScheme. 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 hw1.scm. Click on the Run button.

  2. In the Interactions window, type in the number 7, then hit the Enter key. DrScheme 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 DrScheme.

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

  3. In class on Thursday and Friday we played with some built-in functions in the image.ss teachpack. From the Languages menu, choose Add Teachpack. Select the teachpack image.ss, then hit OK. Now click on the Run button. You should now see a message in the Interactions window indicating that the teachpack image.ss has been loaded.

  4. From the Help menu, choose Help Desk. At the bottom of the window, you'll see "Find docs for". Type in image. Then click on image.ss. Here you will find a description of all the pre-defined functions (operators) in the image.ss teachpack.

  5. 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 symbols, '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).

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

  7. 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 image you created in Step 5. Hit the Run button. In the Interactions window, type TRAFFIC-LIGHT.

  8. 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:
    ;; mod-traffic-light:  symbol symbol symbol -> image
    ;; consumes three symbols 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 mod-traffic-light. (Hint: here is the first line of the function definition, with parameter names top, middle, and bottom:)
    (define (mod-traffic-light top middle bottom)
    

  9. Hit the Run button. If DrScheme 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 mod-traffic-light to create an image of a traffic light. Call the function with arguments 'chartreuse, 'magenta, and 'brown.

  10. Look at your function call from Step 8. Is your function call an expression? Why or why not? (Write your answers as a comment in the Definitions window.)

  11. Answer these questions (write your answers as comments in the Definitions window):

    1. Write an atomic expression that does not involve the use of any numbers.

    2. Define a constant called TAX-RATE that sets the tax rate at 5.75%.

    3. Define a function named taxes-owed that consumes a number representing income and produces a number which is the product of income times the TAX-RATE.

    4. Give the name(s) of any parameters you defined in the function taxes-owed.

    5. Write three different function calls for the function taxes-owed .

    6. List the argument(s) in each of the function calls in the previous problem.


What to Turn In

You should turn in a file called hw1.scm that contains the answers to the problems in Steps 6, 7, 9, and 10. From the File menu, choose Save Definitions. Use web-based turnin to turn in your file before 11:59pm on Sunday, August 26. Solutions to hw1 will be available online in the Schedule section of the class webpage on Monday morning. No late assignments will be accepted.