CSC 123/252 F# Quiz WITH SOLUTIONS** 1. Explain the difference, if any, between let f x y = x+y : int -> (int -> int) Curried and let f(x,y) = x+y : (int*int) -> int Uncurried If they are different, give an example to indicate how they will behave differently, i.e, give an expression that will produce different results. 2. Given: type 'a stack = Emp | Apnd of 'a stack * 'a;; // 'Apnd' for "append" let s = Apnd(Apnd(Apnd(Emp,2),3),5);; let rec tolist = function | Emp -> [] | Apnd(s,a) -> a::tolist(s);; let t = tolist s;; //////// NOT tolist [] s 2a. What are the *types* of s and of t as inferred by F#. s:int stack t: int list 2b. Determine the output of the following: Console.WriteLine( string(t) ); // will print contents of list t. [5;3;2] 3. Given: type operand = Immediate of int | Register of string;; type operation = Push of operand | Pop of operand;; let inst1 = Push(Immediate 3) let inst2 = Pop(Register "ax") Write a function 'tostring' that converts operations into string format. For example: Console.WriteLine(tostring(inst1)); // should print "push 3" Console.WriteLine(tostring(inst2)); // should print "pop ax" Hints: string(3) will return "3". "a" + "b" will return "ab" You can define other functions to help tostring, or do it "all in one." let tostring = function | (Push(Immediate n)) -> "push " + string(n) | (Push(Register r)) -> "push " + r | (Pop(Immediate n)) -> "pop " + string(n) | (Pop(Register r)) -> "pop " + r;;