diff --git a/td3/td3.md b/td3/td3.md index 6e1deba..df8038e 100644 --- a/td3/td3.md +++ b/td3/td3.md @@ -56,3 +56,39 @@ let rec bezout a b = match a mod b with c+(d*r), d ;; ``` + +## Exercice 4 + +```ocaml +let rec ligne n = if n > 0 then ( + print_string "*"; + ligne (n-1) + ) +;; + +let rec escalier n = if n > 0 then ( + escalier (n-1); + ligne n; + print_newline () + ) +;; +``` + +## Exercice 5 + +```ocaml +let rec ligne n s = if n > 0 then ( + print_string s; + ligne (n-1) s + ) +;; + +let rec triangle_h n m = if m > 0 then ( + triangle_h n (m-1); + ligne (n-m) " "; + ligne ((2*m)-1) "*"; + ligne (n-m) " "; + print_newline () + ) +;; +``` diff --git a/td3/td3.ml b/td3/td3.ml new file mode 100644 index 0000000..f2ee915 --- /dev/null +++ b/td3/td3.ml @@ -0,0 +1,6 @@ +let rec ligne n s = if n > 0 then + (print_string s; + ligne (n-1) s) +;; + +let () = ligne 5 "*";; diff --git a/td3/td3.pdf b/td3/td3.pdf index 4712e4a..d210732 100644 Binary files a/td3/td3.pdf and b/td3/td3.pdf differ diff --git a/tp6/dune-project b/tp6/dune-project new file mode 100644 index 0000000..afa291f --- /dev/null +++ b/tp6/dune-project @@ -0,0 +1,26 @@ +(lang dune 3.20) + +(name tp6) + +(generate_opam_files true) + +(source + (github username/reponame)) + +(authors "Author Name ") + +(maintainers "Maintainer Name ") + +(license LICENSE) + +(documentation https://url/to/documentation) + +(package + (name tp6) + (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/tp6/lib/dune b/tp6/lib/dune new file mode 100644 index 0000000..f518b2b --- /dev/null +++ b/tp6/lib/dune @@ -0,0 +1,3 @@ +(library + (name lib) + (public_name tp6)) diff --git a/tp6/lib/lib.ml b/tp6/lib/lib.ml new file mode 100644 index 0000000..e6f6f9f --- /dev/null +++ b/tp6/lib/lib.ml @@ -0,0 +1,127 @@ +type nombre = + Entier of int | + Flottant of float | + Fraction of int * int | + Moins_inf | + Plus_inf;; + +(* Exercice 1 *) + +let correct n = match n with + | Fraction(_, q) -> q > 0 + | _ -> true +;; + +(* Exercice 2 *) +let rec ( $= ) a b = match a, b with + | Fraction(a, b), Fraction(c, d) -> a*d = b*c + | Entier a, Entier b -> a = b + | Flottant a, Flottant b -> a = b + | Entier a, Flottant b -> float_of_int a = b + | Entier a, Fraction(b, c) -> c*a = b + | Flottant a, Fraction(b, c) -> a = (float_of_int b) /. (float_of_int c) + | (Moins_inf | Plus_inf), _ -> false + | a, b -> b $= a +;; + +(* Exercice 3 *) +let rec ( $< ) a b = match a, b with + | Fraction(a, b), Fraction(c, d) -> a*d < b*c + | Entier a, Entier b -> a < b + | Flottant a, Flottant b -> a < b + | Entier a, Flottant b -> float_of_int a < b + | Entier a, Fraction(b, c) -> c*a < b + | Flottant a, Fraction(b, c) -> a < (float_of_int b) /. (float_of_int c) + | Moins_inf, Moins_inf -> false + | Moins_inf, _ -> true + | Plus_inf, Plus_inf -> false + | _, Plus_inf -> true + | a, b -> b $< a +;; + +(* Exercice 4 *) +let rec ($+) a b = match a, b with + | Fraction(a, b), Fraction(c, d) -> Fraction(a*d + b*c, b*d) + | Entier a, Entier b -> Entier(a + b) + | Flottant a, Flottant b -> Flottant(a +. b) + | Entier a, Flottant b -> Flottant(float_of_int a +. b) + | Entier a, Fraction(b, c) -> Fraction(a*c + b, c) + | Flottant a, Fraction(b, c) -> Flottant(a +. ((float_of_int b) /. (float_of_int c))) + | Moins_inf, Plus_inf -> failwith "Cannot add -infinity and +infinity" + | Moins_inf, _ -> Moins_inf + | _, Plus_inf -> Plus_inf + | a, b -> b $+ a +;; + +(* Exercice 5 *) +type valeur = Sept | Huit | Neuf | Dix | Valet | Dame | Roi | As;; +type couleur = Trefle | Pique | Coeur | Carreau;; +type carte = { va : valeur; coul : couleur };; + +let coul_i c = match c with + | Trefle -> 0 + | Pique -> 1 + | Coeur -> 2 + | Carreau -> 3 +;; + +let count_coul hand = Array.fold_left + (fun acc x -> + let () = acc.(coul_i x.coul) <- acc.(coul_i x.coul)+1 in + acc + ) + [|0;0;0;0|] + hand +;; + +(* Exercice 6 *) +let points_carte carte = match carte.va with +| As -> 11 +| Dix -> 10 +| Roi -> 4 +| Dame -> 3 +| Valet -> 2 +| _ -> 0;; + +let hand_points hand = Array.fold_left + (fun acc x -> + acc + points_carte x + ) + 0 + hand +;; + +(* Exercice 7 *) +let val_i v = match v with + | Sept -> 0 + | Huit -> 1 + | Neuf -> 2 + | Dix -> 3 + | Valet -> 4 + | Dame -> 5 + | Roi -> 6 + | As -> 7 +;; + +let count_val hand = Array.fold_left + (fun acc x -> + let () = acc.(val_i x.va) <- acc.(val_i x.va)+1 in + acc + ) + [|0;0;0;0;0;0;0;0|] + hand +;; + +(* Exercice 8 *) +let has_series hand = ( + Array.fold_left + (fun acc x -> + if x = 0 && acc < 5 then + 0 + else + acc + 1 + ) + 0 + (count_val hand) + ) >= 5 +;; diff --git a/tp6/test/dune b/tp6/test/dune new file mode 100644 index 0000000..772bb80 --- /dev/null +++ b/tp6/test/dune @@ -0,0 +1,4 @@ +(executable + (name test_tp6) + (public_name test_tp6) + (libraries lib)) diff --git a/tp6/test/test_tp6.ml b/tp6/test/test_tp6.ml new file mode 100644 index 0000000..59fe078 --- /dev/null +++ b/tp6/test/test_tp6.ml @@ -0,0 +1,17 @@ +open Lib;; + +assert (correct (Fraction (3, 4)));; +assert (correct (Entier 5));; +assert (not (correct (Fraction (3, 0))));; + +assert (Fraction(1, 2) $= Fraction(2, 4));; + +assert (count_coul [| + { coul = Carreau; va = Huit }; + { coul = Carreau; va = Neuf } +|] = [|0;0;0;2|]);; + +assert (count_val [| + { coul = Carreau; va = Huit }; + { coul = Carreau; va = Neuf } +|] = [|0;1;1;0;0;0;0;0|]);; diff --git a/tp6/tp6.opam b/tp6/tp6.opam new file mode 100644 index 0000000..a43bbcb --- /dev/null +++ b/tp6/tp6.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)"]