diff --git a/dm4/bin/dune b/dm4/bin/dune index 1f3ac32..f606d05 100644 --- a/dm4/bin/dune +++ b/dm4/bin/dune @@ -1,4 +1,4 @@ (executable (public_name dm4) (name main) - (libraries dm4)) + (libraries dm4)) \ No newline at end of file diff --git a/dm4/bin/main.ml b/dm4/bin/main.ml index 2860d26..78e0a8f 100644 --- a/dm4/bin/main.ml +++ b/dm4/bin/main.ml @@ -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])); *) *) \ No newline at end of file diff --git a/dm4/lib/dune b/dm4/lib/dune index 485ff58..3247f9b 100644 --- a/dm4/lib/dune +++ b/dm4/lib/dune @@ -1,2 +1,3 @@ (library - (name dm4)) + (name lib) + (public_name dm4)) diff --git a/dm4/lib/lib.ml b/dm4/lib/lib.ml new file mode 100644 index 0000000..aff1634 --- /dev/null +++ b/dm4/lib/lib.ml @@ -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)) \ No newline at end of file diff --git a/dm4/test/dune b/dm4/test/dune index 49f0800..b0b4a2f 100644 --- a/dm4/test/dune +++ b/dm4/test/dune @@ -1,2 +1,3 @@ -(test - (name test_dm4)) +(executable + (name test_dm4) + (libraries lib)) diff --git a/dm4/test/test_dm4.ml b/dm4/test/test_dm4.ml index e69de29..35c13ef 100644 --- a/dm4/test/test_dm4.ml +++ b/dm4/test/test_dm4.ml @@ -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 [[]; []] = []);; \ No newline at end of file