(****************) (* JL - 12/2/17 *) (****************) let liste = [1; 6; 3; 8];; let liste2 = [5; 2; 7; 4];; (** Exercice 1 *) let rec list_length liste = match liste with | [] -> 0 | _ :: queue -> 1 + list_length queue ;; (** Exercice 2 *) let rec concat liste1 liste2 = match liste1 with | [] -> liste2 | tete :: queue -> tete :: concat queue liste2 ;; (******************************************************************) (** Exercice 3 *) let rec maxi liste = match liste with | [] -> failwith "Liste vide" | tete :: [] -> tete | tete :: queue -> max tete (maxi queue) ;; (** Exercice 4 *) let rec element_numero n liste = match n, liste with | _, [] -> failwith "n trop grand ou liste vide" | 1, tete :: _ -> tete | _, _ :: queue -> element_numero (n - 1) queue ;; (** Exercice 5 *) let rec liste_arithm n a b = match n with | 0 -> [] | _ -> match liste_arithm (n - 1) a b with | [] -> [a] | tete :: queue -> (tete + b) :: tete :: queue ;; (** Exercice 6 *) let rec map f liste = match liste with | [] -> [] | tete :: queue -> (f tete) :: map f queue ;; (** Exercice 7 *) let rec decouple liste = match liste with | [] -> ([], []) | (a, b) :: queue -> let liste1, liste2 = decouple queue in (a :: liste1, b :: liste2) ;; (** Exercice 8 *) let rec for_all predicat liste = match liste with | [] -> true | tete :: queue -> (predicat tete) && for_all predicat queue ;; (******************************************************************) (** Tri insertion *) let rec insere x liste = match liste with | [] -> [x] | tete :: _ when x <= tete -> x :: liste | tete :: queue -> tete :: insere x queue ;; let rec tri_insertion liste = match liste with | [] -> [] | tete :: queue -> insere tete (tri_insertion queue) ;; (** Tri bulle *) let rec parcours liste = match liste with | [] -> liste, false | tete :: [] -> liste, false | tete1 :: tete2 :: queue -> if tete1 < tete2 then let liste, changement = parcours (tete2 :: queue) in tete1 :: liste, changement else let liste, _ = parcours (tete1 :: queue) in tete2 :: liste, true let rec tri_bulle liste = let liste, changement = parcours liste in if changement then tri_bulle liste else liste (******************************************************************) (** Tours de Hanoļ *) let deplacement n origine arrivee = "Deplacer le disque " ^ string_of_int n ^ " de " ^ origine ^ " a " ^ arrivee ^ "." let rec hanoi_liste n debut fin intermediaire liste_depl = if n = 0 then liste_depl else let liste1 = hanoi_liste (n - 1) debut intermediaire fin liste_depl in let liste2 = deplacement n debut fin :: liste1 in hanoi_liste (n - 1) intermediaire fin debut liste2 ;; let rev liste = let rec rev_append liste1 liste2 = match liste1 with | [] -> liste2 | tete :: queue -> rev_aux queue (tete :: liste2) in rev_aux liste [] ;; let hanoi n = rev (hanoi_liste n "gauche" "droite" "milieu" []) ;; hanoi 3;;