*** COMPOSED TYPES *** Try to predict the type of every expression below, verifying the answers interactively. ("two", true, 2);; [("two", true, 2)];; ((1,2),(3,4,5));; [[1;2];[3;4;5]];; [[];[];[]];; [1;2;(3,4)];; ('a', "tom"::"jerry"::[], 2<3);; ############################################################################### *** VALUE BINDING AND PATTERN MATCHING *** 1) Try to redict the result of the folloving value binding, verifying the answers interactively. let (a,b) = (5,6);; let (c,d) = (2,("xx","yy"));; let (e,(f,g)) = (1,(2,3));; let (l,m,n) = ("xx",(1,2));; let o = ("z",(3,4));; let (p, _ ) = (12, 10);; let (q, 10) = (12, 10);; let (r, 11) = (12, 10);; let u = 1::[2;3];; let v::w = 1::[2,3];; let v::w = 1::2::3::[];; let h::t = [4;5;6];; 2) Try to predict the result of the following expressions, verifying the answers interactively. let x=4 and y=5+x;; let a=1 in let a=2 and b=a in a+b;; 3) Find the pattern for the following expressions that allow to bind the variable x with the value 0. For example, for an expression (3, true, 0) the following binding is used: let (_,_,x) = (3,true,0);; [1;2;33;0;4] [(1,2);(0,1)] (("a","b"), (0,"aa",[5;3])) ################################################################################ *** FUNCTIONS *** 1) Try to predict the type of the following functions, verifying the answers interactively. fun x -> [x+1;x+2;x+3];; fun x -> (x,x,x);; fun (x,y) -> [x ^ "b"; y];; fun (x,y,z) -> (x+(String.length y),z);; 2) Define the type of the predefined functions "List.rev", "List.hd" and "List.tl" with the help of the OCaml interpreter. Try to predict the results of the following expressions, verifying the answers interactively. List.hd [1;2;3];; List.hd (List.tl [1;2;3]);; List.hd(List.rev [1;2;3]);; List.hd(List.tl(List.rev [1;2;3;4]));; 3) Define the functions "perimeter" and "area" that calculate the perimeter and area of the rectangular triangle given the mesures of the catets. 4) Consider the folloing definition of the function. let rec t = function 0 -> 0 | n -> 2 + t(n-1);; What does this function calculate? 5) Try to evaluate using the interpreter of OCaml the following functions. Then, try it on some input. Be sure that you understood: - what the function is doing - how does the function work let rec d = function 0 -> "de" | n -> "do"^d(n-1)^"da";; let rec h = function 0 -> 1 | n -> h(n-1)+h(n-1);; Come si potrebbe ottimizzare h? let rec m = function (a,0) -> 0 | (a,b) -> a+m(a,b-1);; let rec f = function 0 -> true | n -> not(f(n-1));; let rec g = function 0 -> [] | n -> n::g(n-1);; let rec l = function 0 -> [] | n -> n mod 10 :: l(n/10);; let rec j = function 0 -> 0 | n -> n mod 10 + n/10;; 6) Define a function "list3" that returns a list that contains doubled values of the three given inputs. For example, list3 [1;2;3] = [2;4;6] 7) Define the following functions using the pattern-matching if possible. sumto n = 1 + 2 + ... + n listfrom n = [n;n-1;...;0] strcopy("ab",n) = "ababab..." (n volte) (usare la funzione predefinita "^" per concatenare stringhe) power(x,n) = x**n listcopy(a,n) = [a;a;...;a] (n volte) sumEvens n = 0 + 2 + ... + n (n pari) listOdds n = [n; n-2; ...; 1] (n dispari) nat(n) = "succ(...(succ(zero)))" (concatenare n volte "succ") listTo(n) = [1;..;n]