From 9b544600d8dac92399e3aaac4eebbf47a0e950bb Mon Sep 17 00:00:00 2001 From: Francesco Minnocci Date: Sun, 24 Apr 2022 00:58:05 +0200 Subject: [PATCH] Cleanup, update --- .gitignore | 1 + esperimenti/esperimenti.fsproj | 12 ---- fib/Program.fs | 7 ++ fib/fib.fsproj | 12 ---- lab3/lab3.fsproj | 12 ---- lab4 (+5)/Program.fs | 90 ++++++++++++++++++++++++++ ludo/ludo.fsproj | 12 ---- ludo2/Program.fs | 113 +++++++++++++++++++++++++++++++++ mc91/mc91.fsproj | 12 ---- 9 files changed, 211 insertions(+), 60 deletions(-) delete mode 100644 esperimenti/esperimenti.fsproj delete mode 100644 fib/fib.fsproj delete mode 100644 lab3/lab3.fsproj create mode 100644 lab4 (+5)/Program.fs delete mode 100644 ludo/ludo.fsproj create mode 100644 ludo2/Program.fs delete mode 100644 mc91/mc91.fsproj diff --git a/.gitignore b/.gitignore index 7cc460d..ea98503 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ */obj */.ionide */.config +*/*.fsproj diff --git a/esperimenti/esperimenti.fsproj b/esperimenti/esperimenti.fsproj deleted file mode 100644 index b3c42bf..0000000 --- a/esperimenti/esperimenti.fsproj +++ /dev/null @@ -1,12 +0,0 @@ - - - - Exe - net6.0 - - - - - - - diff --git a/fib/Program.fs b/fib/Program.fs index ad7d986..6dafdf4 100644 --- a/fib/Program.fs +++ b/fib/Program.fs @@ -8,7 +8,14 @@ let rec fib n = | 1 -> 1 | _ -> fib (n-1) + fib (n-2) +let rec fibSmart n = + match n with + | n when n < 0 -> failwith (sprintf "Error: %d is less than 0" n) + | (0|1) -> n + | _ -> fibSmart (n-1) + fibSmart (n-2) + // Read from stdin and print result printfn "Inserisci l'indice del numero di fibonacci che vuoi calcolare:\n" let i = Console.ReadLine() |> int printfn "%A" (fib (i)) +printfn "%A" (fibSmart (i)) diff --git a/fib/fib.fsproj b/fib/fib.fsproj deleted file mode 100644 index b3c42bf..0000000 --- a/fib/fib.fsproj +++ /dev/null @@ -1,12 +0,0 @@ - - - - Exe - net6.0 - - - - - - - diff --git a/lab3/lab3.fsproj b/lab3/lab3.fsproj deleted file mode 100644 index b3c42bf..0000000 --- a/lab3/lab3.fsproj +++ /dev/null @@ -1,12 +0,0 @@ - - - - Exe - net6.0 - - - - - - - diff --git a/lab4 (+5)/Program.fs b/lab4 (+5)/Program.fs new file mode 100644 index 0000000..dd51a39 --- /dev/null +++ b/lab4 (+5)/Program.fs @@ -0,0 +1,90 @@ +// 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 []) diff --git a/ludo/ludo.fsproj b/ludo/ludo.fsproj deleted file mode 100644 index b3c42bf..0000000 --- a/ludo/ludo.fsproj +++ /dev/null @@ -1,12 +0,0 @@ - - - - Exe - net6.0 - - - - - - - diff --git a/ludo2/Program.fs b/ludo2/Program.fs new file mode 100644 index 0000000..f994299 --- /dev/null +++ b/ludo2/Program.fs @@ -0,0 +1,113 @@ +let rec foldLeft g f k1 k2 z = + if k1 >= k2 then g(z,f(k1)) + else g(foldLeft g f k1 (k2-1) z, f k2) + + +let rec foldRight g f k1 k2 z = + if k1 >= k2 then g(f(k1),z) + else g(f k1, foldRight g f (k1+1) k2 z) + + + +let list1 = [1; 2; 3] +let list2 = [1,2,3] +let list2' = [(1,2,3)] +let list3 = (1,2,3) +let list4 = [] +let list5 = 4::list1 + +let rec lngth lst = + if lst = [] + then 0 + else 1 + (lngth (List.tail lst)) + +//printfn "%A" (lngth [1;3;4]) + +let rec invList l i = + if l = [] then [] + else if ((List.length l) - i) = 0 then [List.head l] + else + l[List.length l - i] :: (invList l (i+1)) + +//printfn "%A" (invList [1;2;3;4] 1) + +let reverseList l = + let rec reverseL acc l = + if l = [] then acc + else + reverseL ( (List.head l) :: acc) (List.tail l) + reverseL [] l + +//printfn "%A" (reverseList [1;2;3;4]) + +let rec sumList (l: int list) = + if l = [] then 0 + else + List.head l + sumList (List.tail l) + +let rec maxL (l : int list) (max : int) = + if l = [] then max + else + if (List.head l) > max then maxL (List.tail l) (List.head l) + else maxL (List.tail l) max + +let maxList l = if l= [] then failwith "Lista vuota" else maxL l (List.head l) + +//printfn "%A" (sumList [1;2;3;4]) + +//printfn "%A" (maxList [1;2;3;4]) + +//printfn "%A" (maxList []) + +let rec fib x = + match x with + | 1 -> 1 + | 2 -> 1 + | n -> fib (n-1) + fib (n-2) + +let rec fib2 x = + match x with + | n when n < 1 -> failwith (sprintf "Error: %d is less than 1" x) + | (1|2) -> 1 + | n -> fib (n-1) + fib (n-2) + +let rec fn2 a = + match a with + | (x,y) when x > 0 && y > 0 -> x + y + | (x,_) when x < 0 -> 0 + | _ -> failwith "This makes no sense" + +let rec length2 l = + match l with + | [] -> 0 + | _ :: xs -> 1 + (length2 xs) + +let rec nth i l = + match (i,l) with + | (_,[]) -> failwith "Errore: ricorsione termina in lista vuota" + | (0, x :: _) -> x + | (n, _ :: xs) when n > 0 -> nth (n-1) xs + | _ -> failwith "Errore: indice negativo" + +// printfn "%A" (nth 4 [1;2;3;4;5]) + +let rec EuclidsAlg n m = + match (a,b) with + | _ when (a <= 0 || b <= 0) -> failwith "Errore: almeno un numero non positivo" + | _ when (a = b) -> a + | _ when (a > b) -> EuclidsAlg (a-b) b + | _ -> EuclidsAlg a (b-a) + +let rec unzip l = + match l with + | [] -> ([],[]) + | (a,b) :: xs -> (a :: (fst (unzip xs)), b :: (snd (unzip xs)));; + +// printfn "%A" (EuclidsAlg 48 12) + +let rec unzip' l = + match l with + | [] -> ([],[]) + | (a,b) :: xs -> + let (aa,bb) = unzip xs + (a :: aa, b :: bb) \ No newline at end of file diff --git a/mc91/mc91.fsproj b/mc91/mc91.fsproj deleted file mode 100644 index b3c42bf..0000000 --- a/mc91/mc91.fsproj +++ /dev/null @@ -1,12 +0,0 @@ - - - - Exe - net6.0 - - - - - - -