Cleanup, update
parent
e63c20719f
commit
9b544600d8
@ -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<y -> 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 [])
|
@ -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)
|
Loading…
Reference in New Issue