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
- Generic program design recipe.
- 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"
- 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.
- 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].
- 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?
- prefixSums returns a list of numbers.
- The number in the first
position of the list is the sum of the prefix of the input with length 1.
- The number in the second
position of the list is the sum of the prefix of the input with length 2.
- 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]
- The prefix of the input with length 1 is [2].
- The prefix of the input with length 2 is [2,6].
- The prefix of the input with length 3 is [2,6,7].
- 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.
- 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:
- take 0 [3,4,5] --> []
- take 6 [3,4] --> [3,4]
- take 3 [2,5,6,7,8] --> [2,5,6]
- 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:
- Your name (as the author of the program) indicated in a comment in the very first line
- At least 5 function definitions, one for each of the assigned functions.
You may define additional functions to help, or to create tests.
- Products for each of the concrete steps in the generic design recipe
- Understand the problem
- Write a contract (type) for each function
- Create a set of examples (HUnit assertions)
- Create a body for each function where the structure of the body mirrors the structure of the input data.
- Tests for each your functions (HUnit tests)
- Any products from the recursive design recipe you deem to be usefull to the reader of your program.
Upload this file using the blackboard assignment
mechanism.
Back to the Daily Record.
Back to the class web-page.