(* TP PF sujet n°3 *) (* -------------------------------------------------------- *) (* I *) (* (a) *) let concat_string l = List.fold_right (^) l "";; (* autre version possible *) let concat_string = List.fold_left (^) "";; let pairs l = List.fold_right (function x -> function l -> if (x mod 2) = 0 then (x::l) else l) l [];; let map f l = List.fold_right (function x -> function l -> (f x)::l) l [];; (* (b) *) let rec for_all p l = match(l) with [] -> invalid_arg "for_all" | a::[] -> (p a) | a::r -> (p a) & (for_all p r);; let rec exists p l = match(l) with [] -> invalid_arg "exists" | a::[] -> (p a) | a::r -> (p a) or (exists p r);; let tous_pairs = for_all (function n -> n mod 2 = 0);; (* ou bien *) let tous_pairs l = not (exists (function n -> n mod 2 = 1) l);; (* (c) *) let rec cribble = function [] -> [] | t::r -> t::(cribble (List.fold_right (function x -> function l -> if (x mod t) = 0 then l else (x::l) ) r []) );; cribble [2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20;21;22;23];; (* - : int list = [2; 3; 5; 7; 11; 13; 17; 19; 23] *) (* -------------------------------------------------------- *) (* II *) (* (a) *) #load "dessin.cma";; open Dessin;; (* (b) *) let carre h co (Point (x,y)) d = let sommetbg = Point (x,y) in let sommethd = Point (x+h,y+h) in let sommethg = Point (x,y+h) in let sommetbd = Point (x+h,y) in trait sommetbg sommethg co (trait sommethg sommethd co (trait sommethd sommetbd co (trait sommetbd sommetbg co d)));; (* exemple : *) fenetre (carre 20 Bleu (Point (10,10)));; (* (c) *) let maison h co (Point (x,y)) d = let sommetbg = Point (x,y) in let sommethd = Point (x+h,y+h) in let sommethg = Point (x,y+h) in let sommetbd = Point (x+h,y) in let sommettoit = Point (x+h/2,y+3*h/2) in trait sommetbg sommethd co (trait sommethd sommethg co (trait sommethg sommettoit co (trait sommettoit sommethd co (trait sommethd sommetbd co (trait sommetbd sommethg co (trait sommethg sommetbg co (trait sommetbg sommetbd co d)))))));; (* exemple : *) fenetre (maison 20 Vert (Point (10,10)));; (* (d) *) type direction = Vecteur of int * int;; type etat = Triplet of position * direction * dessin;; let au_bord (Point (x,y)) = (x<=0) or (x>=799) or (y<=0) or (y>=599);; let autre_direction (Triplet (Point (x,y), Vecteur (dirx,diry), d)) = let dirx' = if (x<=0) then 1 else if (x>=799) then -1 else dirx in let diry' = if (y=0) then 1 else if (y>=599) then -1 else diry in Vecteur (dirx',diry');; let bouge (Triplet (Point (x,y), Vecteur (dirx,diry), d)) = let d0 = carre 20 Blanc (Point (x,y)) d in let (x0,y0) = (x+dirx,y+diry) in if au_bord (Point (x0,y0)) then ( let Vecteur (dirx0,diry0) = autre_direction ( Triplet ( Point (x0,y0), Vecteur (dirx,diry), d0 ) ) in Triplet ( Point (x+dirx0,y+dirx0), Vecteur (dirx0,diry0), (carre 20 Noir (Point (x+dirx0,y+dirx0)) d0) ) ) else ( Triplet ( Point (x0,y0), Vecteur (dirx,diry), (carre 20 Noir (Point (x0,y0)) d0) ) );; let frein = (* fonction identité (utilisée pour ralentir le programme) *) let rec compteur n = if n=0 then 0 else compteur(n-1) in function x-> if (compteur 10000)=0 then x else x;; let rec trajectoire n etat = if n=0 then etat else trajectoire (n-1) (frein (bouge etat));; (* exemple d'utilisation *) let etat_initial d = Triplet (Point (0,0), Vecteur (1,1), (carre 20 Noir (Point (0,0)) d)) ;; fenetre (function d0 -> let Triplet (pos,dir,dn) = trajectoire 5000 (etat_initial d0) in dn);;