full tp4 + start of dm4

This commit is contained in:
Arkitu 2025-11-07 11:51:11 +01:00
parent d3c9e61f90
commit eef0427ee7
19 changed files with 219 additions and 0 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
**/exe
_build/

4
dm4/bin/dune Normal file
View File

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

4
dm4/bin/main.ml Normal file
View File

@ -0,0 +1,4 @@
let plusetfois a b = (a+b, a*b);;
assert (plusetfois 5 6 == (11, 30));;

32
dm4/dm4.opam Normal file
View 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)"]

26
dm4/dune-project Normal file
View File

@ -0,0 +1,26 @@
(lang dune 3.20)
(name dm4)
(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 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

2
dm4/lib/dune Normal file
View File

@ -0,0 +1,2 @@
(library
(name dm4))

2
dm4/test/dune Normal file
View File

@ -0,0 +1,2 @@
(test
(name test_dm4))

0
dm4/test/test_dm4.ml Normal file
View File

15
tp4/bin/dune Normal file
View File

@ -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))

33
tp4/bin/exo1.ml Normal file
View File

@ -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]));;

10
tp4/bin/exo2.ml Normal file
View File

@ -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);;

3
tp4/bin/exo3.ml Normal file
View File

@ -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 );

6
tp4/bin/exo4.ml Normal file
View File

@ -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.]));;

18
tp4/bin/exo5.ml Normal file
View File

@ -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]));;

26
tp4/dune-project Normal file
View File

@ -0,0 +1,26 @@
(lang dune 3.20)
(name tp4)
(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 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

3
tp4/lib/dune Normal file
View File

@ -0,0 +1,3 @@
(library
(name exo1)
)

2
tp4/test/dune Normal file
View File

@ -0,0 +1,2 @@
(test
(name test_tp4))

0
tp4/test/test_tp4.ml Normal file
View File

32
tp4/tp4.opam Normal file
View 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)"]