Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

26/10/2022 14:26 https://universitice.univ-rouen.fr/pluginfile.php/1634671/mod_resource/content/2/correction_cc_2021_1.

ml

(* Exercice 1 *)

fun f x y -> if (f x) then 2 else y;;

(* - : ('a -> bool) -> 'a -> int -> int = <fun> *)

fun f x y -> f (x,y);;

(* - : ('a * 'b -> 'c) -> 'a -> 'b -> 'c = <fun> *)

fun x y z -> (z y,z x);;

(* - : 'a -> 'a -> ('a -> 'b) -> 'b * 'b = <fun> *)

fun x y -> x (x y);;

(* - : ('a -> 'a) -> 'a -> 'a = <fun> *)

fun x y -> x (y x);;

(* - : ('a -> 'b) -> (('a -> 'b) -> 'a) -> 'b = <fun> *)

(fun x y z -> (y x) ** 2. +. z) 2;;

(* - : (int -> float) -> float -> float = <fun> *)

fun (x,y,z) -> fun x -> (x@y, y, z);;

(* - : 'a * 'b list * 'c -> 'b list -> 'b list * 'b list * 'c = <fun> *)

fun f x ->
if fst (f (fst x)) = (snd x)

then (snd (f (fst x)))

else (snd (f (fst x)));;

(* - : ('a -> 'b * 'c) -> 'a * 'b -> 'c = <fun> *)

(* Exercice 2 *)

List.map ((+) 1) [3; 11; 17; 21; 5];;

(* - : int list = [4; 12; 18; 22; 6] *)

List.fold_left (fun a b -> if b > a then b else a) 0 [5; 1; 10; 2];;

(* - : int = 10 *)

List.filter (fun (_,x) -> x <> 0 ) [(1,0); (2,1); (3,0); (0,4)];;

(* - : (int * int) list = [(2, 1); (0, 4)] *)

(* Exercice 3 *)

let projection c = List.rev (List.rev_map fst c);;

projection [(1, 2); (3, 4); (5,6)];;

(* - : int list = [1; 3; 5] *)

let renverse l = List .fold_left (fun x y -> y::x) [] l;;

renverse [1; 2; 3];;

(* - : int list = [3; 2; 1] *)

let suffixes l = List.fold_left (fun a _ -> List.tl (List.hd a) :: a) [l] l;;

(* ou encore *)

let suffixes l = [] :: List.rev_map (List.rev) (List.fold_left (fun s e -> (List.rev_map


(function l -> e :: l) ([] :: s))) [] l) ;;

suffixes [1; 2; 3];;

(* - : int list list = [[]; [3]; [2; 3]; [1; 2; 3]] *)

let prefixes l = renverse (List.rev_map renverse (suffixes (renverse l)));;

prefixes [1; 2; 3];;

(* - : int list list = [[]; [1]; [1; 2]; [1; 2; 3]] *)

https://universitice.univ-rouen.fr/pluginfile.php/1634671/mod_resource/content/2/correction_cc_2021_1.ml 1/1

You might also like