You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
113 lines
2.6 KiB
Forth
113 lines
2.6 KiB
Forth
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) |