Instructions: This problem set has three parts; you will write each in a separate file. Each file should have your name, netID, and what section you're in in a comment at the top. All programs that you submit must compile. Programs that do not compile will receive an automatic zero. If you are having trouble getting your assignment to compile, please visit consulting hours. If you run out of time, it is better to comment out the parts that do not compile and hand in a file that compiles, rather than handing in a more complete file that does not compile. Also, please continue to pay attention to style. Refer to the CS 312 style guide and lecture notes. Take the extra time to think out the problems and find the most elegant solutions before coding them up. As before, it is important that you do not change the names of the functions or structures.
When submitting your implementations for the following problems, you should be sure to include all of the code that you used to test your implementation. The completeness of these test cases will be a part of your grade. Even if your solution to a problem is well-written and correct, lack of exhaustive test cases could cost you points.
fun fold (f:'b*'a*'b->'b) (empty_value:'b) (t:'a tree):'b =
...
Where empty_value is the value used in place of
fold(empty). Once fold is implemented properly, we can do:
fun max(a:int,b:int,c:int):int = ...
fold max 0 tree
This will find the maximum value in the tree, using 0 as the maximum
value for the empty tree (so, if all values are negative, the result
will be 0). In addition to implementing fold so it works with the max
function, you should also use fold to build a string that lists the
values of all the nodes in an int tree by writing a function
concat3:string*int*string->string.
fun concat (a:int,b:string) = b^Int.toString(a)
(* inorderFoldl f b tree = f(6,f(3,f(5,f(1,f(2,f(4,b)))))) *)
inorderFoldl concat "" tree = "421536"
inorderFoldr concat "" tree = "635124"
LIS([1,2,3,4]) = [1,2,3,4]
LIS([4,3,2,1]) = [3]
LIS([1,2,2,2]) = [1,2]
LIS([5,2,7,8,3,2,5,7,8,3]) = [2,3,5,7,8]
In addition to your solution (which can be implemented in under 20
lines of code), you should include a comment analyzing the complexity
of your implemenation. Your complexity should be no worse than
O(N3), where N is the number of elements in the input to
LIS. You will receive 9/10 points for a correct implementation which
you correctly analyze as having a complexity of O(N3). A
better solution has complexity of only O(N2), which is
required to receive the full 10 points.
xi = (xi-1*a+b) mod ca, b, and c are all constants and their selection plays an important role in the quality of the random number generator. After you get your number generator to work, you should experiment with it, and try to find good values for a, b, and c. In particular, try plotting xi vs i -- the result should show no obvious pattern.
datatype rand = Rand of (int*(unit->rand))Then, we can write a function to generate our random number generating function:
fun rand_gen(seed:int):unit->rand =
...
Once, you've implemented this function, you should be able to make a
list of random numbers as follows:
fun rand_list(seed:int,cnt:int):int list =
let val rg = rand_gen(seed)
fun go(f:unit->rand,cnt:int) =
if (cnt = 0) then []
else
let
val Rand(a,b) = f()
in
a::go(b,cnt-1)
end
in
go(rg,cnt)
end