Comp 200 Homework 1:
Finger Exercises and Programming Intro

Due in class on Wed 30 Jan 2002

  1. First, you need to do some account-setup.

  2. Look at the class newsgroup (link from the main class web page), find the posting titled Homework 1 Question (Spring 2002), and send me an e-mail with the response to that question. If you're doing the assignment with a partner, I expect an e-mail from both of you. (details on reading news)

  3. Start DrScheme (details), and experimentally type a few things into the top half, and press "execute". (Nothing to turn in.)

  4. What do the following Scheme expressions evaluate to (when executed in this order)? Verify by typing them into Dr.Scheme, but first try to predict the answer.

    1. (+ 22 (* 4 7) -2)
    2. (- 4 3 2)
    3. (define pi 3)
    4. pi
    5. (define (square n) (* n n))
    6. (square 7)
    7. (define (area r) (* pi r r))
    8. (area pi)
    9. area
    10. (and true (or false true))
    11. (if false (if false 3 4) (if true 5 6))
    12. (if (= 3 2) (/ 1 0) (/ 1 2))
    13. (+ false true)

    You can write your answers by hand, or, if you type these in DrScheme's definitions window (the top half), then you can (1) cut/paste the outputs from the interactions window up into the definitions and "print definitions window", or (2) you can just "print interactions window". (Note: If you decide to cut/paste into the definitions window, it's probably best to write in a #| before the block of answer text and a |# afterwards. This "comments out" this portion of the text, so Scheme's interpreter doesn't see it as code. This way, if you save the file, but find a need to execute it again, this text won't produce an error when interpreted. To comment out a single line of text rather than an entire block, put a ; at the beginning of the line.)

  5. Write Scheme versions of the following mathematical formulae, and test each on at least two inputs (one of which should be some sort of borderline case, e.g. 32 degrees fahrenheit).

    1. celc( f ) = (5/9)(f-32)
    2. dist( a, t ) = (1/2) a t²
    3. my-abs( x ) = x, if x >= 0, or -x if x < 0.

    Here, similar to one of the options above, type these in the definitions window, including test cases; then cut/paste the results up to the top half, commenting them out appropriately, and print the definitions.

  6. Write Scheme functions that do the following:

    (These may get tricky, but I have faith in you! Two heads are better than one, so it's certainly to your advantage to work in pairs. Don't forget, you can always post questions to the newsgroup if you need clarification or help with a particular problem.)

    1. Write a function (rect-area l w) that calculates the area of a rectangle, given the length and width of the rectangle as inputs.
    2. Given the hypotenuse length of an ISOSCELES RIGHT triangle, write a function (tri-side hyp) that calculates the length of one of the other sides. (Remember the Pythagorean Theorem?)
    3. Suppose we worked at a flag making company and needed to know the area that the white part of one Swiss flag covers, that is, the area of the cross. Given (1) the vertical height (same as the horizontal width) of the cross and (2) the length of the cross's diagonal line of symmetry (measured from the upper-left inward-pointing corner to the lower-right inward-pointing corner only), write a function (cross-area height sym-line) that calculates the area of the cross. (Click here for an image and description of the Swiss flag.)
    4. Given the information from the previous problem, as well as the length of one side of the flag, write a function (red-area cross-height cross-sym-line flag-length) that calculates the area of the red part of the Swiss flag. (The flag is square.)
    5. Finally, given the same three inputs from the previous problem, write a predicate function (cross-area-greater? cross-height cross-sym-line flag-length) that returns true if the area of the cross is greater than the area of the red part and false otherwise.

    Like before, show at least two test cases for each one.