(*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*) (* JL *) (* 28 January 2017 + 9 February 2020 *) (*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*) (**********************************) (* 1. Fonctions d'ordre supérieur *) (**********************************) (** Exercice 1 *) let min x y = if x < y then x else y ;; let max x y = if x > y then x else y ;; max 0;; min 0.;; (* (max 0) est de type int -> int et est la fonction "partie positive" entière : appliquée à un entier k, elle renvoie k si k positif et 0 sinon. *) (* (min 0.) est de type float -> float et est la fonction qui à un flottant x associe 0. s'il est négatif et x sinon. *) let min_bis x y = -(max (-x) (-y));; (* Par rapport à la fonction polymorphe précédente, ici, on force le type à être entier *) min_bis 0.;; (** Exercice 2 *) let snd = fun (a, b) -> b;; let snd (a, b) = b;; let snd couple = let (a, b) = couple in a ;; (** Exercice 3 *) (* duplicate de type 'a -> 'a * 'a *) let duplicate a = (a, a);; (* swap de type 'a * 'b -> 'b * 'a *) let swap (a, b) = (b, a);; (* concat de type 'a * 'b -> 'c * 'd -> 'a * 'b * 'c * 'd *) let concat (a, b) (c, d) = (a, b, c, d);; (** Exercice 4 *) (* fonction1 de type (int -> 'a) -> 'a est la fonction F : f |-> f(0). *) (* fonction2 est exactement la même fonction. *) (* fonction3 de type ('a -> int) -> ('a -> int) -> 'a -> int est la fonction G : f |-> (g |-> f + g)). *) (* fonction4 devrait être la même fonction sans la faute de frappe. Ici, elle est de type ('a -> int) -> 'b -> 'a -> int. Il s'agit de la fonction H : f |-> (g |-> 2 f). *) (** Exercice 5 *) let moy f = ((f 0.) +. (f 1.)) /. 2.;; let carre f x = (f x) ** 2.;; let itere f = fun x -> f (f x);; let itere2 f x = f (f x);; (* définition équivalente à la précédente *) let f_de_x_plus_1 f x = f (x +. 1.);; (** Exercice 6 *) let g x = let f x = sin (log x) in (f x) /. (1. +. (f (x +. 1.)) ** 2.) ;; (** Exercice 7 *) (* min ne peut pas s'appliquer sur des fonctions que OCaml ne sait pas comparer. *) let f = fun x -> x + 1;; let g = fun x -> 2 * x;; let h = fun x -> min (f x) (g x);; (** Exercice 8 *) let sh x = (exp x -. exp (-.x)) /. 2.;; let ch x = exp x -. sh x;; let th x = sh x /. ch x;; let argsh x = log (x +. sqrt (1. +. x ** 2.));; let argch x = log (x +. sqrt (x ** 2. -. 1.));; let argth x = (log ((1. +. x) /. (1. -. x))) /. 2.;;