CS 334
|
I would like you to turn in paper and electronic versions of your programs. To turn in individual files, be sure you are in the directory containing the file and then type
turnin filenamewhere filename is the name of your file. It will then prompt you for the course. Please type 334, and your file will be deposited properly. You may turnin as many files as you like as long as they have different names. Please use names that indicate the assignment and problem number(s). Please make sure that your programs will run if we "use" them from within sml. Thus enclose problem numbers, etc. in (* ... *) so they will be treated as comments. Please also turn in well-documented(!) print-outs of programs as well as the answers to non-programming questions. (Among other things, please make sure that identifier names are intelligible so that I can understand what each stands for in the programs.)
Problems to be turned in
fun digit_to_char (n:int) = chr (n + ord #"0");Use a let clause to hide the auxiliary functions.
- zip [1,3,5,7] ["a","b","c","de"]; val it = [(1,"a"),(3,"b"),(5,"c"),(7,"de")]: (int * string) listNote: If the vectors don't have the same length, you may decide how you would like the function to behave. If you don't specify any behavior at all you will get a warning from the compiler that you have not taken care of all possible patterns.
- unzip [(1,"a"),(3,"b"),(5,"c"),(7,"de")]; val it = ([1,3,5,7], ["a","b","c","de"]): int list * string list
fun exp base power = if power = 0 then 1 else base * (exp base (power-1));There are more efficient means of exponentiation. First write an ML function that squares an integer, and then using this function, design an ML function fastexp which calculates (basepower) for any power >= 0 by the rule
base0 = 1 basepower = (base(power/2))2 if power is even basepower = base * (base(power-1)) if power is oddIt is easy to blow this optimization and not save any multiplications. Prove the program you implemented is indeed faster than the original by comparing the number of multiplications that must be done for exponent m in the first and second algorithms. (Hint it is easiest to calculate the number of multiplications if the exponent, power, is of the form 2k for the second algorithm. Give the answer for exponents of this form and then try to give an upper bound for the others. Also recall that ML is call-by-value. That is, the argument to a function is evaluated before the call.)
find (7, [1,5,7,2])would return 2. First write a definition for find where the element is guaranteed to be in the list. Now modify your definition so that it returns ~1 if the element is not in the list.
datatype 'a tree = Leaf of 'a | InternalNode of ('a tree) * ('a tree)