Informatique/MPSI/spe/TP1/main.ml

80 lines
1.8 KiB
OCaml
Raw Permalink Normal View History

2020-01-24 11:26:40 +01:00
(* Exercice 1 *)
(*1)*)
() ;;
(*2)*)
let c2() = 2;;
(*3)*)
let rien a = ();;
(*4)*)
let hello() = print_string "Hello World !\n";;
(*Exercice 2*)
let rec itere f n a = match n with
| 0 -> a
| _ -> f (itere f (n-1) a);;
itere sin 3000 1.;;
(* Exercice 3*)
(*1) : La fonction List.length retourne la longueur d'une liste.*)
let rec longueur liste = match liste with
| [] -> 0
| _ -> (longueur (List.tl liste)) + 1;;
longueur [1;2;3];;
(*2) : La fonction List.map applique une fonction <20> chaque <20>l<EFBFBD>ment d'une liste.*)
let rec mapper f liste = match liste with
| [] -> []
| _ -> (f (List.hd liste))::(mapper f (List.tl liste));;
mapper sin [1.;2.;3.];;
(*3)*)
let rec concatener listes = match longueur listes with
| 0 -> []
| 1 -> List.hd listes
| 2 -> (List.hd listes)@(List.hd (List.tl listes))
| _ -> concatener ([(List.hd listes)@(List.hd (List.tl listes))]@(List.tl (List.tl listes)));;
2020-01-28 10:10:46 +01:00
concatener [[1;2;3];[2;1;3];[];[4;5;1;3];[1]];;
2020-01-24 11:26:40 +01:00
concatener [];;
2020-01-27 19:29:42 +01:00
List.concat [];;
(* exercice 4*)
2020-01-28 10:10:46 +01:00
let rec recherche_dicho d f a v =
let c = (f - d)/2 in
if c = 0 then false
else if a = v.(c) then true
else if a < v.(c)
then recherche_dicho d c a v
else recherche_dicho c f a v;;
2020-01-27 19:29:42 +01:00
2020-01-28 10:10:46 +01:00
let recherche_dichotomique a v =
recherche_dicho 0 (Array.length v) a v;;
2020-01-27 19:29:42 +01:00
2020-05-02 18:15:27 +02:00
recherche_dichotomique (-5) [|1;2;3;4;5;6;7;8;9;10;5|];;
2020-01-27 19:29:42 +01:00
(*Exercice 5*)
2020-01-28 10:10:46 +01:00
let est_premier n = (*on prend un n >=2*)
2020-01-27 19:29:42 +01:00
let t = ref true in
if n = 2 then ()
else for i = 2 to n/2 do
if (n mod i) = 0 then t := false
done ;
!t;;
2020-01-28 10:10:46 +01:00
est_premier 17;;
est_premier 14;;
2020-01-27 19:29:42 +01:00
2020-01-28 10:10:46 +01:00
(*let est_vrm_premier n = (* on prend aussi un n >=2*)
2020-01-27 19:29:42 +01:00
let t = ref true and i = ref 2 in
while !i <=(n/2) && !t do
if (n mod !i) = 0 then t := false;
i := !i + 1
done;
2020-01-28 10:10:46 +01:00
!t;;*)