HMWK1
Due: 1 March 2018, by end of class
1) Write a function called "ncopies", with the following type spec:
ncopies: string -> int -> string list
here is a couple of sample runs:
- ncopies 0 "x"
val it = [] : string list
- ncopies 4 "x"
val it = [ "x", "x", "x", "x"] : string list
2) Given the following data type:
datatype cTree = EMPTY |
NODE of string * int * cTree * cTree;
(where the node basically holds a string, and a count of how many times the string occurs)
you may presume that the append function is available (as defined in class).
Write a function called detree with the type spec:
detree : cTree -> string list
with the following sample runs:
- detree (NODE("x",2,(NODE("w",1,EMPTY,EMPTY),(NODE("y",3,EMPTY,EMPTY)));
val it = ["w", "x", "x", "y", "y", "y"] : string list
- detree EMPTY;
val it = [] : string list
3) Given a funtion called "concat", with type spec concat: string list -> string
that works like this:
- concat ["x", "y", "z"];
val it = "xyz": string
and the functions:
fun curry f x y = f (x,y);
fun uncurry f (x,y) = f x y;
Determine the type specs of:
a) fun smush (a,b) = concat [a,b];
b) val smush2 = curry smush;
4) Finally, write a function which inserts strings into the cTree datatype.
Your function should have this typespec: string -> cTree -> cTree
Note: i) the strings should be stored in-order in the tree.
ii) inserting another copy of a string should just increase the count field on that node.