CS 1101 - C-term 10

Homework 0 - Warming up in Scheme


Assignment Goals

You will not be turning in your solutions to this assignment (it won't be graded). However, I will expect that you will know all the material covered in this assignment by Tuesday, January 19.


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.

  1. Start up DrScheme. If this is the first time you've brought up DrScheme, you will be asked to choose a language. From the Language menu, choose Choose Language. Double-click on "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 hw0.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. 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 teachpack. Notice the search box at the top of the page. Type in overlay/xy to find information about that operator.

  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:
    ;; 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 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)
    

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

  11. Answer these questions:

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

    3. Define a constant called EXEMPTION-AMOUNT that is defined as 700 (dollars).

    4. Define a function named taxes-owed that consumes a number representing income and a number representing the number of dependents and produces a number. The number produced is the difference between the product of the income and the TAX-RATE, and the product of the number of dependents and EXEMPTION-AMOUNT.

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

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

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


What to Turn In

You do not have to turn in anything for this assignment.