tp6 q1-8
This commit is contained in:
parent
f91821c946
commit
aec4f1bcce
36
td3/td3.md
36
td3/td3.md
@ -56,3 +56,39 @@ let rec bezout a b = match a mod b with
|
|||||||
c+(d*r), d
|
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 ()
|
||||||
|
)
|
||||||
|
;;
|
||||||
|
```
|
||||||
|
|||||||
6
td3/td3.ml
Normal file
6
td3/td3.ml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
let rec ligne n s = if n > 0 then
|
||||||
|
(print_string s;
|
||||||
|
ligne (n-1) s)
|
||||||
|
;;
|
||||||
|
|
||||||
|
let () = ligne 5 "*";;
|
||||||
BIN
td3/td3.pdf
BIN
td3/td3.pdf
Binary file not shown.
26
tp6/dune-project
Normal file
26
tp6/dune-project
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
(lang dune 3.20)
|
||||||
|
|
||||||
|
(name tp6)
|
||||||
|
|
||||||
|
(generate_opam_files true)
|
||||||
|
|
||||||
|
(source
|
||||||
|
(github username/reponame))
|
||||||
|
|
||||||
|
(authors "Author Name <author@example.com>")
|
||||||
|
|
||||||
|
(maintainers "Maintainer Name <maintainer@example.com>")
|
||||||
|
|
||||||
|
(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
|
||||||
3
tp6/lib/dune
Normal file
3
tp6/lib/dune
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
(library
|
||||||
|
(name lib)
|
||||||
|
(public_name tp6))
|
||||||
127
tp6/lib/lib.ml
Normal file
127
tp6/lib/lib.ml
Normal file
@ -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
|
||||||
|
;;
|
||||||
4
tp6/test/dune
Normal file
4
tp6/test/dune
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
(executable
|
||||||
|
(name test_tp6)
|
||||||
|
(public_name test_tp6)
|
||||||
|
(libraries lib))
|
||||||
17
tp6/test/test_tp6.ml
Normal file
17
tp6/test/test_tp6.ml
Normal file
@ -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|]);;
|
||||||
32
tp6/tp6.opam
Normal file
32
tp6/tp6.opam
Normal file
@ -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 <maintainer@example.com>"]
|
||||||
|
authors: ["Author Name <author@example.com>"]
|
||||||
|
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)"]
|
||||||
Loading…
Reference in New Issue
Block a user