// list of 3 integers let list1 = [1; 2; 3] // list of 1 triple of integers let list2 = [1,2,3] printfn "%b" (list2 = [(1,2,3)]) // -> true // questa non è una lista(!) let list3 = (1,2,3) let rec len l = if l = [] then 0 else 1 + (len (List.tail l)) printfn "%A" (len [1; 2; 3; 4]) let rec revList l c = if l = [] then [] else if List.length l - c = 0 then [List.head l] else l.[List.length l - c] :: (revList l (c+1)) printfn "%A" (revList [1; 2; 3; 4] 1) printfn "%A" (revList [1] 1) printfn "%A" (revList [] 1) let rec sumList (l : int list) = if l = [] then 0 else List.head l + (sumList (List.tail l)) printfn "%A" (sumList [1; 2; 3; 4]) let rec maxList (l : int list) currentMax = if l = [] then currentMax else if (List.head l > currentMax) then maxList (List.tail l) (List.head l) else maxList (List.tail l) currentMax printfn "%A" (maxList [1; 2; 3; 4] 0) let rec nth l n = match (l, n) with | ([], _) -> failwith "Empty list" | (x::_, 0) -> x | (_::tail, n) when n > 0 -> nth tail (n-1) | _ -> failwith "n is negative" printfn "%A" (nth [1; 2; 3; 4] 0) printfn "%A" (nth [1; 2; 3; 4] 3) let rec gcd m n = match (m,n) with | (x,y) when x<=0 || y<=0 -> failwith "Non-positive number inserted" | (x,y) when x>y -> gcd (x-y) y | (x,y) when x gcd x (y-x) | _ -> m printfn "%A" (gcd 2048 1234) let rec zip l l' = match (l,l') with | ([],[]) -> [] | (x::xs,y::ys) -> (x,y)::(zip xs ys) | _ -> failwith "Lists have different lengths" let rec zipAndCut l l' = match (l,l') with | ([],_)|(_,[]) -> [] | (x::xs,y::ys) -> (x,y)::(zip xs ys) let rec unzip (l: ('a*'b) list ) = match l with | [] -> ([],[]) | (a,b)::l' -> let (h,k)=(unzip l') (a::h,b::k) printfn "%A" (unzip [(0,0);(1,1);(2,1);(3,2);(4,3);(5,5);(6,8);(7,13)]) printfn "%A" (unzip [])