Homework One
Finger Exercises

Comp 210

Due: At the beginning of class on 98.Sep.09 (Wed.)

These exercises are to be done with DrScheme. When writing functions (problems 2 and 3, and the rest of the semester):

  1. (3pts) For each of the following expressions, give its value (if any). If the expression is illegal (i.e. drScheme gave an error message), explain why (in about three words). What you turn in should have both the expression and the resulting value. For example: (* 3 2) = 6. This problem can be entirely hand-written.
    1. (+ 22 10 -2)
    2. (- 20 (* 3 5) 2)
    3. (/ 44 14)
    4. (sqrt 16)
    5. (sqrt 16 25)
    6. (zero? (+ 2 -2))
    7. (and (= 3 3) (zero? 17))
    8. (and #t (or #f #t))
    9. (if (> 3 4) (/ 1 0) (/ 1 9))
    10. (if #f (if #f 3 4) (if #t 5 6))
    11. (+ #f #t)
    12. (if 3 4 5)
    13. (if #t 7)
    14. (define pi 3)
    15. (define area (lambda (r) (* pi r r)))
    16. (area 4)
    17. (lambda (x) (* 2 pi x))
    If you don't understand why drScheme gives each of the answers it does, be sure to talk it over with a friend or labby.

    To think about only (not to be graded): At Beginner level, DrScheme gives an error when it sees two open-parentheses in a row, since this is not something beginners want to do. However, consider the following expression closely: ((lambda (x) (* 2 pi x)) 4). Do you feel this should be an allowable expression? If so, what should its value be? In the future, when we raise the language level, you can see what DrScheme thinks.

  2. (3pts) 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). Include a contract preceding the function.
    1. celc( f ) = (5/9)(f-32)
    2. dist( a, t ) = 1/2 a t²
    3. root1( a, b, c ) = [-b - sqrt(b²-4ac)]/2a, where it is guaranteed that a is not zero.
    4. my-abs( x ) = x, if x >= 0, or -x if x < 0.

  3. (4pts) Houston drivers love to drive fast. Most drivers accelerate quickly from a standing stop to the de-facto speed limit of 80 miles per hour (mph) and then maintain that constant speed. Write a Scheme function that takes two inputs:
    1. the acceleration (in mph per second) of a car, and
    2. an elapsed driving time in seconds
    and returns the distance traveled (in feet) by the car. Use several helper functions to aid in making your program more readable. All functions should have a contract! (This requirement will always apply for this class, and won't be repeated any more.) (If you want, you may presume that the acceleration is greater than zero, so that the cruising-speed is eventually reached. On the other hand, it is clear what the answer should be, if the acceleration is zero...)
  4. Hint: Most physics book contain a formula relating acceleration to distance traveled. You should use any applicable function already written for a previous exercise. You may wish to double-check your solution against these inputs:
    (houston-driver-distance 6 5) = 110
    (houston-driver-distance 10 10) = 704

    Also test your function on two or three other values. (What borderline cases are there?)