Homework #1 - Design Recipes - Recursive functions over lists

In this home work we will practice our skills on using design recipes and writing recursive functions over lists.

Recall that every step in a design recipe cooresponds to a product. It must be the case that product appears in your solution. It could be a comment, or some actual program fragment. Study the design recipes

  1. Generic program design recipe.
  2. Recursive program design recipe.
and the example program we created in class to see what kjind of things should appear in your file.

For this homework you will create a haskell program which defines five functions. Study the problem description below for each function. This problem description could form the the basis of the product for the generic program design step "understand the problem"

  1. addOne is a list to list function. Its input is a list of Int, and its output is a list of Int. The output can be obtained from the input by adding 1 to each element. For example: addOne [2,6,7] --> [3,7,8]. Be sure and consider each step in Generic program design recipe and the Recursive program design recipe.

  2. addN is a generalization of addOne. It is a list to list function. Its inputs are and Int and a list of Int, and its output is a list of Int. The output can be obtained from the input by adding the first input to each element of the second input. For example: addN 3 [2,6,7] --> [5,9,10].

  3. interest takes a Double as input and returns a Double as output. The output is the yearly interest rate that the National Bank of Haskell pays on a deposit the size of the input. The more you deposit the greater the interest rate the bank pays.
    +-------------------+-------------+
    |deposit amount     |interest rate|
    +-------------------+-------------+
    |0 < x <= 500       |3.0%         |
    +-------------------+-------------+
    |500 < x <= 5000    |3.2%         |
    +-------------------+-------------+
    |5000 < x <= 20000  |3.6%         |
    +-------------------+-------------+
    |20000 < x <= 100000|4.0%         |
    +-------------------+-------------+
    |100000 < x         |4.2%         |
    +-------------------+-------------+
    
    Think carefully about the structure of the input. The input structure has 5 cases, so the structure of the function should have 5 cases. Is there more than one way to do this?

  4. prefixSums returns a list of numbers.
    1. The number in the first position of the list is the sum of the prefix of the input with length 1.
    2. The number in the second position of the list is the sum of the prefix of the input with length 2.
    3. The number in the third position of the list is the sum of the prefix of the input with length 3, etc.

    This is best illustrated with an example. If the input is [2,6,7,5]

    1. The prefix of the input with length 1 is [2].
    2. The prefix of the input with length 2 is [2,6].
    3. The prefix of the input with length 3 is [2,6,7].
    4. The prefix of the input with length 4 is [2,6,7,5].

    Thus, the result is [2,2+6, 2+6+7, 2+6+7+5] --> [2,8,15,20].

    Hint: the function addN might be useful. Be sure and write this function recursively.

  5. take returns a prefix of a list. The first input is the length of the prefix, and the second input is the list that the prefix is obtained from. Its possible that the list is not long enough to obtain a prefix of the supplied length. In that case return the longest possible prefix. For example:

    1. take 0 [3,4,5] --> []
    2. take 6 [3,4] --> [3,4]
    3. take 3 [2,5,6,7,8] --> [2,5,6]
    4. take 2 [] --> []

    Hint: this function decomposes both its arguments to make recursive calls.

What to turn in.

Create a Haskell source file. Upload the file to blackboard by class time October 8, 2009. The file must have: Upload this file using the blackboard assignment mechanism.

Back to the Daily Record.

Back to the class web-page.