CS 536 (F02) Homework 1: Warming up in Scheme

Due: September 10 in class (hardcopy)


Assignment Goals


Note

Many of these exercises are taken directly from the on-line text How to Design Programs. We depart from the book in one major way: instead of using define-struct and cond for compound data and variants, we use define-datatype and cases. Documentation for these constructs can be found in DrScheme's Help Desk; here's a small example:

(define-datatype vehicle vehicle?
  [taxi]
  [bus (seats number?)
       (hand-holds number?)
       (color string?)]
  [subway (cars number?)
          (seats-per-car number?)
          (line symbol?)])

(define (max-occupants v)
  (cases vehicle v
    [taxi () 4]
    [bus (seats handholds color) (+ seats hand-holds)]
    [subway (cars seats-per-car line) (* cars seats-per-car)]))

(max-occupants (subway 10 25 'green))
> 250

Exercises

  1. (HtDP Exercise 2.3.2) The local supermarket needs a program that can compute the value of a bag of coins. Define the program sum-coins. It consumes four numbers: the number of pennies, nickels, dimes, and quarters in the bag; it produces the amount of money in the bag.

  2. (HtDP Exercise 4.4.2) Develop the function tax, which consumes the gross pay and produces the amount of tax owed. For a gross pay of $240 or less, the tax is 0%; for over $240 and $480 or less, the tax rate is 15%; and for any pay over $480, the tax rate is 28%.

    Also develop netpay. The function determines the net pay of an employee from the number of hours worked. The net pay is the gross pay minus the tax. Assume the hourly pay rate is $12.

    Hint: Remember to develop auxiliary functions when a definition becomes too large or too complex to manage.

  3. Define the function suffixes, which consumes a list l, and produces a list of all suffixes of l. For example:

        (suffixes '(a b c d))
        > ((a b c d) (b c d) (c d) (d) ())
    
  4. Define a datatype for toys. A toy is one of:

  5. Develop the function bear-sale that consumes a list of toys and returns a list of toys. The returned list has the same toys as the input list, but the prices on all bears have been discounted 20%.

  6. (HtDP Exercise 10.1.5) Develop the function eliminate-exp to eliminate expensive toys. The function consumes a number, called ua, and a list of toys and produces a list of all those toys that are below or equal to ua in price.

  7. Develop the function tall-programmable? that consumes a list of toys and returns a boolean indicating whether the list contains a programmable robot more than 3 feet tall.

  8. Define a datatype for a family tree. A family tree is either:

    For example, a small family tree looks like:

        
        (person "Dave" 1977 'brown
                (person "Ken" 1945 'brown
                        (unknown)
                        (unknown))
                (person "Mary Ellen" 1946 'brown
                        (unknown)
                        (unknown)))
    

  9. (HtDP Exercise 14.1.3) Develop count-persons. The function consumes a family tree node and produces the number of people in the corresponding family tree.

  10. (HtDP Exercise 14.1.4) Develop the function average-age. It consumes a family tree node and the current year. It produces the average age of all people in the family tree.

  11. (HtDP Exercise 14.1.5) Develop the function eye-colors, which consumes a family tree node and produces a list of all eye colors in the tree. An eye color may occur more than once in the list.

    Hint: Use the Scheme operation append, which consumes two lists and produces the concatenation of the two lists. For example:

    (append (list 'a 'b 'c) (list 'd 'e)) = (list 'a 'b 'c 'd 'e)


    Back to the Assignments page