This commit is contained in:
Arkitu 2025-11-08 20:43:00 +01:00
parent eef0427ee7
commit 3dcc1c886b
6 changed files with 113 additions and 6 deletions

View File

@ -1,4 +1,4 @@
(executable
(public_name dm4)
(name main)
(libraries dm4))
(libraries dm4))

View File

@ -1,4 +1,28 @@
(* let plusetfois a b = (a+b, a*b);;
(* assert (plusetfois 5 6 == (11, 30));; *)
let plusetfois a b = (a+b, a*b);;
let rec pp2_h n = match n with
| 0 -> 1
| _ -> 2 * (pp2_h (n/2))
;;
let pp2 n = if n == 0 then 0 else pp2_h (n-1);;
assert (plusetfois 5 6 == (11, 30));;
(* assert (pp2 10 == 16);;
assert (pp2 32 == 32);;
assert (pp2 1 == 1);;
assert (pp2 0 == 0);; *)
let leeloo l = match l with
| _::_::_::_::a::_ -> a
| _ -> failwith "Not enough elements";;
(* assert (leeloo [2; 65; 2; 3; 7; 2] == 7);; *)
let rec coupe l = match l with
| a::b::q ->
let qa, qb = coupe q in
a::qa, b::qb
| _ -> l, []
;;
(* assert (coupe [2; 65; 4; 3; 7; 2] == ([2; 4; 7], [65; 3; 2])); *) *)

View File

@ -1,2 +1,3 @@
(library
(name dm4))
(name lib)
(public_name dm4))

46
dm4/lib/lib.ml Normal file
View File

@ -0,0 +1,46 @@
let plusetfois a b = (a+b, a*b);;
let rec pp2_h n = match n with
| 0 -> 1
| _ -> 2 * (pp2_h (n/2))
let pp2 n = if n == 0 then 0 else pp2_h (n-1)
let leeloo l = match l with
| _::_::_::_::a::_ -> a
| _ -> failwith "Not enough elements"
let rec coupe l = match l with
| a::b::q ->
let qa, qb = coupe q in
a::qa, b::qb
| _ -> l, []
let rec colle la lb = match la with
| [] -> lb
| a::q -> a::(colle lb q)
let rec compte l x = match l with
| [] -> 0
| a::q -> if a == x then 1+(compte q x) else compte q x
let rec sommesi l f = match l with
| [] -> 0
| a::q -> if f a then a+(sommesi q f) else (sommesi q f)
let rec majopred_h l f acc acc_n = match l with
| [] -> 2*acc >= acc_n
| a::q -> majopred_h q f (if f a then acc+1 else acc) (acc_n+1)
let majopred l f = majopred_h l f 0 0
let rec lexico la lb = match la, lb with
| [], _ -> true
| _, [] -> false
| a::_, b::_ when a < b -> true
| a::_, b::_ when a > b -> false
| _::qa, _::qb -> lexico qa qb
let rec aplatir l = match l with
| [] -> []
| []::ql -> aplatir ql
| (a::q)::ql -> a::(aplatir (q::ql))

View File

@ -1,2 +1,3 @@
(test
(name test_dm4))
(executable
(name test_dm4)
(libraries lib))

View File

@ -0,0 +1,35 @@
open Lib;;
assert (plusetfois 5 6 = (11, 30));;
assert (pp2 10 = 16);;
assert (pp2 32 = 32);;
assert (pp2 1 = 1);;
assert (pp2 0 = 0);;
assert (leeloo [2; 65; 2; 3; 7; 2] = 7);;
assert (coupe [2; 65; 4; 3; 7; 2] = ([2; 4; 7], [65; 3; 2]));;
assert (coupe [2; 65; 4; 3; 7; 2; 0] = ([2; 4; 7; 0], [65; 3; 2]));;
assert (colle [2; 4; 7] [65; 3; 2] = [2; 65; 4; 3; 7; 2]);;
assert (colle [2; 4; 7; 0] [65; 3; 2] = [2; 65; 4; 3; 7; 2; 0]);;
assert (compte [2; 65; 4; 3; 7; 2] 2 = 2);;
assert (sommesi [2; 65; 4; 3; 7; 2] (fun x -> x mod 2 = 1) = 75);;
assert (majopred [2; 65; 4; 3; 7; 2] (fun x -> x mod 2 = 1));;
assert (not (majopred [2; 65; 6; 4; 3; 7; 2] (fun x -> x mod 2 = 1)));;
assert (lexico [2; 4; 7] [2; 4; 7]);;
assert (not (lexico [2; 4; 7; 3] [2; 4; 7]));;
assert (lexico [2; 4; 7] [2; 4; 7; 3]);;
assert (lexico [2; 4; 7] [2; 5; 7]);;
assert (not(lexico [2; 5; 7] [2; 4; 7]));;
assert (aplatir [[1; 2]; [3; 4]] = [1; 2; 3; 4]);;
assert (aplatir [[]; [3; 4]] = [3; 4]);;
assert (aplatir [] = []);;
assert (aplatir [[]] = []);;
assert (aplatir [[]; []] = []);;