(*propo de correction pour le tp1 de LP2*) (*LG/MP janvier 2007*) (*Partie 1*) (*exo 1*) let f1 x y z = x*y*z;; (*val f1 : int -> int -> int -> int = *) let g1 x y = x*y ;; (*val g1 : int -> int -> int = *) let f2 x y z = x+y+z;; let g2 x y = x+y ;; let foo ope x y z = ope (ope x y) z;; (*val foo : ('a -> 'b -> 'a) -> 'a -> 'b -> 'b -> 'a = *) (*exo2*) let h x = x;; (*val h : 'a -> 'a = *) let hh x y = y x ;; (*val hh : 'a -> ('a -> 'b) -> 'b = *) let hhh x = x x ;; (*This expression has type 'a -> 'b but is here used with type 'a*) let ff fonc = fonc 0;; (* val ff : (int -> 'a) -> 'a = *) let gg fonc couple = fonc (fst couple + snd couple );; (* val gg : (int -> 'a) -> int * int -> 'a = *) (*exo 3*) let f i = print_int i; fun x-> x+i;; (*val f : int -> int -> int = *) let g =f 2;; (* 2val g : int -> int = *) g 5;; (* - : int = 7 *) let plus x y = x+y;; (*pas possible autrement*) let mult x y = x*y;; let h1 = foo plus;; let h2 = foo mult;; h2 3 4 5 ;; (* - : int = 60 *) (*exo 4*) let facto x1 = let rec facto_aux x accu= if x=1 then accu else facto_aux (x-1) (accu*x) in facto_aux x1 1;; facto 6;; (* - : int = 720 *) (* val facto : int -> int = *) let fibo x1 = let rec fib_aux x ad d = if x<=1 then d else fib_aux (x-1) d (ad+d) in fib_aux x1 1 1;; (* val fibo : int -> int = *) fibo 4;; (* - : int = 5 *) (*Partie 2*) (*exo 5*) type integer_list = Vide | Cons of int * integer_list;; (* type integer_list = Vide | Cons of int * integer_list *) let l1 = Vide;; (* val l1 : integer_list = Vide *) let l2 = Cons(2,Cons(9,Cons(1,Vide)));; (* val l2 : integer_list = Cons (2, Cons (9, Cons (1, Vide) *) let l3 = Cons(1515,Cons(-4,Cons(1,Cons(-4,Vide))));; (* val l3 : integer_list = Cons (1515, Cons (-4, Cons (1, Cons (-4, Vide)))) *) (*exo 6*) let rec length ll = match ll with | Vide -> 0 | Cons(a,reste) -> 1+ length reste;; (* val length : integer_list -> int = *) length l3;; (* - : int = 4 *) let rec equal l1 l2 = match l2 with | Vide -> l1 = Vide | Cons(a,reste) -> match l1 with | Cons(b,reste2) when a=b -> equal reste reste2 | _ -> false;; (* val equal : integer_list -> integer_list -> bool = *) equal l1 l1 ;; equal l1 l2;; equal l2 l3;; (* - : bool = true - : bool = false - : bool = false *) (*exo 7*) let head l = match l with | Vide -> failwith "empty list" | Cons(a,reste) -> a;; (* val head : integer_list -> int = *) let tail l = match l with | Vide -> failwith "empty list" | Cons(a,reste) -> reste;; (* val tail : integer_list -> integer_list = *) let rec nth list n = if n<1 then failwith "pb" else if n =1 then head list else nth (tail list) (n-1);; (* val nth : integer_list -> int -> int = *) nth l2 2 ;; (* - : int = 9 *) (*Exo 8*) let inverse l1 = let rec transfert source dest = match source with | Vide -> dest | Cons(a,reste) -> transfert reste (Cons(a,dest)) in transfert l1 Vide;; (* val inverse : integer_list -> integer_list = *) inverse l2;; (* - : integer_list = Cons (1, Cons (9, Cons (2, Vide))) *)