Homework IV

 

 

1. [30 points]

Write and test the definition of a (polymorphic) Haskell function 'center' that takes three arguments, a list arg1 of type [a], a width arg2 of type Int, and a fill item arg3 of type a, and returns a list of length arg2 of type [a] containing list arg1 centered within fill items (i.e., the difference between the number of items preceding arg1 and those following arg1 is at most 1). For instance, center "abcd" 7 '-' could yield "--abcd-" or "-abcd--" (as you choose).

 

2. [30 points]

Write and test the definition of a (polymorphic) Haskell function 'removeDups' that takes a single list argument of type [a] and returns a list with the same items as the argument but with no duplicates; the order of items in the return list should be the same as the order of the first appearance of items in the argument list. For instance, removeDups [5, 2, 5, 3, 5, 1, 3] yields [5,2,3,1].

 

3. [40 points]

Write and test the definition of a (polymorphic) Haskell function 'split' that takes a single list argument of type [a] and returns the list of type ([a], [a]) (i.e., pair of lists) of all distinct pairs of lists that can be concatenated to produce the argument list. For instance, split [1,2,3] should yield a list of four pairs, namely

[([], [1,2,3]), ([1], [2,3]), ([1,2], [3]), ([1,2,3], [])]. The order in which the pairs appear in the result can be chosen at your discretion.

 

Program submission instructions

Program solutions must include documentation (in-line comments, and a separate write-up when appropriate) that makes it clear both what general method you used in constructing the program, and how the details of the program accomplish that method. You need to run test cases that exercise every component of your code, and include documentation that justifies that your test data meets this condition (the 'script' command is normally used to prepare materials). It is not the grader’s responsibility to figure out how you wrote the program and whether it is correct -- it is your responsibility to explain your program and convince the grader it is completely tested and correct. Full credit will not be awarded, even for (apparently) correct programs, without completing these requirements. Note that you are free to add other (auxiliary) functions as you find them suitable, but be sure to test them with the same rigor as the specified functions.

 

In addition, to submitting a printed listing of your source program and test outcomes, you should use the 'submit' command to provide an electronic copy of your source code. For this homework your submit file should include the source code for your three answers. Direct it to the directory Hwk4 for course c111. There is a link to the description of this command on our course Web page, or use Linux help (man submit).