From eef0427ee7f1f7882e370cb7563bf6e82349f043 Mon Sep 17 00:00:00 2001 From: Arkitu Date: Fri, 7 Nov 2025 11:51:11 +0100 Subject: [PATCH] full tp4 + start of dm4 --- .gitignore | 1 + dm4/bin/dune | 4 ++++ dm4/bin/main.ml | 4 ++++ dm4/dm4.opam | 32 ++++++++++++++++++++++++++++++++ dm4/dune-project | 26 ++++++++++++++++++++++++++ dm4/lib/dune | 2 ++ dm4/test/dune | 2 ++ dm4/test/test_dm4.ml | 0 tp4/bin/dune | 15 +++++++++++++++ tp4/bin/exo1.ml | 33 +++++++++++++++++++++++++++++++++ tp4/bin/exo2.ml | 10 ++++++++++ tp4/bin/exo3.ml | 3 +++ tp4/bin/exo4.ml | 6 ++++++ tp4/bin/exo5.ml | 18 ++++++++++++++++++ tp4/dune-project | 26 ++++++++++++++++++++++++++ tp4/lib/dune | 3 +++ tp4/test/dune | 2 ++ tp4/test/test_tp4.ml | 0 tp4/tp4.opam | 32 ++++++++++++++++++++++++++++++++ 19 files changed, 219 insertions(+) create mode 100644 dm4/bin/dune create mode 100644 dm4/bin/main.ml create mode 100644 dm4/dm4.opam create mode 100644 dm4/dune-project create mode 100644 dm4/lib/dune create mode 100644 dm4/test/dune create mode 100644 dm4/test/test_dm4.ml create mode 100644 tp4/bin/dune create mode 100644 tp4/bin/exo1.ml create mode 100644 tp4/bin/exo2.ml create mode 100644 tp4/bin/exo3.ml create mode 100644 tp4/bin/exo4.ml create mode 100644 tp4/bin/exo5.ml create mode 100644 tp4/dune-project create mode 100644 tp4/lib/dune create mode 100644 tp4/test/dune create mode 100644 tp4/test/test_tp4.ml create mode 100644 tp4/tp4.opam diff --git a/.gitignore b/.gitignore index 32c5e2f..933e78a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ **/exe +_build/ diff --git a/dm4/bin/dune b/dm4/bin/dune new file mode 100644 index 0000000..1f3ac32 --- /dev/null +++ b/dm4/bin/dune @@ -0,0 +1,4 @@ +(executable + (public_name dm4) + (name main) + (libraries dm4)) diff --git a/dm4/bin/main.ml b/dm4/bin/main.ml new file mode 100644 index 0000000..2860d26 --- /dev/null +++ b/dm4/bin/main.ml @@ -0,0 +1,4 @@ + +let plusetfois a b = (a+b, a*b);; + +assert (plusetfois 5 6 == (11, 30));; diff --git a/dm4/dm4.opam b/dm4/dm4.opam new file mode 100644 index 0000000..a43bbcb --- /dev/null +++ b/dm4/dm4.opam @@ -0,0 +1,32 @@ +# This file is generated by dune, edit dune-project instead +opam-version: "2.0" +synopsis: "A short synopsis" +description: "A longer description" +maintainer: ["Maintainer Name "] +authors: ["Author Name "] +license: "LICENSE" +tags: ["add topics" "to describe" "your" "project"] +homepage: "https://github.com/username/reponame" +doc: "https://url/to/documentation" +bug-reports: "https://github.com/username/reponame/issues" +depends: [ + "dune" {>= "3.20"} + "ocaml" + "odoc" {with-doc} +] +build: [ + ["dune" "subst"] {dev} + [ + "dune" + "build" + "-p" + name + "-j" + jobs + "@install" + "@runtest" {with-test} + "@doc" {with-doc} + ] +] +dev-repo: "git+https://github.com/username/reponame.git" +x-maintenance-intent: ["(latest)"] diff --git a/dm4/dune-project b/dm4/dune-project new file mode 100644 index 0000000..3e0cb80 --- /dev/null +++ b/dm4/dune-project @@ -0,0 +1,26 @@ +(lang dune 3.20) + +(name dm4) + +(generate_opam_files true) + +(source + (github username/reponame)) + +(authors "Author Name ") + +(maintainers "Maintainer Name ") + +(license LICENSE) + +(documentation https://url/to/documentation) + +(package + (name dm4) + (synopsis "A short synopsis") + (description "A longer description") + (depends ocaml) + (tags + ("add topics" "to describe" your project))) + +; See the complete stanza docs at https://dune.readthedocs.io/en/stable/reference/dune-project/index.html diff --git a/dm4/lib/dune b/dm4/lib/dune new file mode 100644 index 0000000..485ff58 --- /dev/null +++ b/dm4/lib/dune @@ -0,0 +1,2 @@ +(library + (name dm4)) diff --git a/dm4/test/dune b/dm4/test/dune new file mode 100644 index 0000000..49f0800 --- /dev/null +++ b/dm4/test/dune @@ -0,0 +1,2 @@ +(test + (name test_dm4)) diff --git a/dm4/test/test_dm4.ml b/dm4/test/test_dm4.ml new file mode 100644 index 0000000..e69de29 diff --git a/tp4/bin/dune b/tp4/bin/dune new file mode 100644 index 0000000..e2b662a --- /dev/null +++ b/tp4/bin/dune @@ -0,0 +1,15 @@ +(executable + (public_name exo1) + (name exo1)) +(executable + (public_name exo2) + (name exo2)) +(executable + (public_name exo3) + (name exo3)) +(executable + (public_name exo4) + (name exo4)) +(executable + (public_name exo5) + (name exo5)) diff --git a/tp4/bin/exo1.ml b/tp4/bin/exo1.ml new file mode 100644 index 0000000..818b2cf --- /dev/null +++ b/tp4/bin/exo1.ml @@ -0,0 +1,33 @@ +let rec somme_liste l = match l with +| [] -> 0 +| a::q -> a + somme_liste q;; + +assert (somme_liste [1; 3; 2] = 6);; + +let rec taille_liste l = match l with +| [] -> 0 +| _::q -> 1 + taille_liste q;; + +assert (taille_liste [1; 3; 2] = 3);; + +let rec pow valeur exposant = match exposant with +| 0 -> 1 +| i -> valeur * (pow valeur (i - 1));; + +assert (pow 4 3 = 64);; + +(* On peut remplacer i - 1 par exposant - 1 car les deux noms coexistent. *) +let rec part_ent_log2 nombre = + if nombre = 0 then failwith "Boum !"; + (* Il est toujours utile de connaître les exceptions tôt. *) + if nombre = 1 then 0 + else 1 + (part_ent_log2 (nombre / 2));; + +assert (part_ent_log2 10 = 3);; + +let rec est_dans_liste element l = match l with + | [] -> false + | a::q -> a = element || est_dans_liste element q;; + +assert (est_dans_liste 2 [1; 3; 2]);; +assert (not (est_dans_liste 4 [1; 3; 2]));; diff --git a/tp4/bin/exo2.ml b/tp4/bin/exo2.ml new file mode 100644 index 0000000..f31723b --- /dev/null +++ b/tp4/bin/exo2.ml @@ -0,0 +1,10 @@ + +let biggest l = match l with + | [] -> failwith "Empty list" + | x::q -> List.fold_left + (fun acc a -> max acc a) + x + q +;; + +assert (biggest [0; 7; 4] == 7);; diff --git a/tp4/bin/exo3.ml b/tp4/bin/exo3.ml new file mode 100644 index 0000000..2aea6f6 --- /dev/null +++ b/tp4/bin/exo3.ml @@ -0,0 +1,3 @@ +let prod l = List.fold_left (fun acc x -> acc *. x) 1. l;; + +assert (abs_float (prod [1.5; 2.; 3.] -. 9.) <= 1e-14 ); diff --git a/tp4/bin/exo4.ml b/tp4/bin/exo4.ml new file mode 100644 index 0000000..4f8c035 --- /dev/null +++ b/tp4/bin/exo4.ml @@ -0,0 +1,6 @@ +let rec is_ordered l = match l with + | a::b::q -> a <= b && is_ordered (b::q) + | _ -> true;; + +assert (is_ordered [1; 5; 6]);; +assert (not (is_ordered [5.6; 7.8; 3.]));; diff --git a/tp4/bin/exo5.ml b/tp4/bin/exo5.ml new file mode 100644 index 0000000..0c035df --- /dev/null +++ b/tp4/bin/exo5.ml @@ -0,0 +1,18 @@ +let rec is_ordered l = match l with + | a::b::q -> a <= b && is_ordered (b::q) + | _ -> true;; +let rec is_rev_ordered l = match l with + | a::b::q -> a >= b && is_ordered (b::q) + | _ -> true;; + +let rec has_ordering l = match l with + | a::b::q -> if a < b then + is_ordered (b::q) + else if a > b then + is_rev_ordered (b::q) + else has_ordering (b::q) + | _ -> true;; + +assert (has_ordering [1; 1; 2; 2; 3]);; +assert (has_ordering [3; 2; 1; 1]);; +assert (not (has_ordering [1; 1; 2; 1]));; diff --git a/tp4/dune-project b/tp4/dune-project new file mode 100644 index 0000000..2991b88 --- /dev/null +++ b/tp4/dune-project @@ -0,0 +1,26 @@ +(lang dune 3.20) + +(name tp4) + +(generate_opam_files true) + +(source + (github username/reponame)) + +(authors "Author Name ") + +(maintainers "Maintainer Name ") + +(license LICENSE) + +(documentation https://url/to/documentation) + +(package + (name tp4) + (synopsis "A short synopsis") + (description "A longer description") + (depends ocaml) + (tags + ("add topics" "to describe" your project))) + +; See the complete stanza docs at https://dune.readthedocs.io/en/stable/reference/dune-project/index.html diff --git a/tp4/lib/dune b/tp4/lib/dune new file mode 100644 index 0000000..18ed8b1 --- /dev/null +++ b/tp4/lib/dune @@ -0,0 +1,3 @@ +(library + (name exo1) +) diff --git a/tp4/test/dune b/tp4/test/dune new file mode 100644 index 0000000..a495465 --- /dev/null +++ b/tp4/test/dune @@ -0,0 +1,2 @@ +(test + (name test_tp4)) diff --git a/tp4/test/test_tp4.ml b/tp4/test/test_tp4.ml new file mode 100644 index 0000000..e69de29 diff --git a/tp4/tp4.opam b/tp4/tp4.opam new file mode 100644 index 0000000..a43bbcb --- /dev/null +++ b/tp4/tp4.opam @@ -0,0 +1,32 @@ +# This file is generated by dune, edit dune-project instead +opam-version: "2.0" +synopsis: "A short synopsis" +description: "A longer description" +maintainer: ["Maintainer Name "] +authors: ["Author Name "] +license: "LICENSE" +tags: ["add topics" "to describe" "your" "project"] +homepage: "https://github.com/username/reponame" +doc: "https://url/to/documentation" +bug-reports: "https://github.com/username/reponame/issues" +depends: [ + "dune" {>= "3.20"} + "ocaml" + "odoc" {with-doc} +] +build: [ + ["dune" "subst"] {dev} + [ + "dune" + "build" + "-p" + name + "-j" + jobs + "@install" + "@runtest" {with-test} + "@doc" {with-doc} + ] +] +dev-repo: "git+https://github.com/username/reponame.git" +x-maintenance-intent: ["(latest)"]