(********* Exercice 1 *********) let curry f a b = f(a,b);; let uncurry f (a,b) = f a b;; let my_mod = uncurry mod_float;; (* val my_mod : float * float -> float = *) my_mod(5.,3.);; (* - : float = 2. *) let inverse f = (fun x y -> f y x);; let rec iterate f n x = if n = 0 then x else iterate f (n - 1) (f x);; (********* Exercice 2 *********) let sum l = List.fold_left (+) 0 l;; sum [1;2;3;2;1];; let length l = List.fold_left (fun x y -> x + 1) 0 l;; length [1;2;3;2;1];; let max l = match l with | [] -> failwith "Max on an empty list" | x::xs -> List.fold_left max x xs;; max [1;2;3;2;1];; let list_or l = List.fold_left (||) false l;; list_or [];; list_or [true;false];; list_or [false];; (********* Exercice 3 *********) interface Criterion { public abstract Boolean test(Guitar g); default Criterion and(Criterion other) { return (g) -> { return this.test(g) && other.test(g); }; } } class CFactory { private static Map> cache = new HashMap<>( Map.ofEntries( Map.entry("Trademark", (Object t) -> (g) -> { return Trademark.isSame(g.getTrademark(), (Trademark) t); }), Map.entry("Kind", (Object k) -> (g) -> { return Kind.isSame(g.getKind(), (Kind) k); }), Map.entry("Model", (Object m) -> (g) -> { return Guitar.isSameModel(g, (String) m); }) )); public static Criterion makeCriterion(String name, Object value) { Function fact = cache.get(name); return (fact == null) ? null : fact.apply(value); } public static void addCriterionFactory(String name, Function fact) { cache.put(name, fact); } } static List search3(Criterion crit) { List matchingGuitars = new ArrayList<>(); for (Guitar guitar : guitars) { if (crit.test(guitar)) matchingGuitars.add(guitar); } return matchingGuitars; } CFactory.addCriterionFactory("Price<=", (Object d) -> (g) -> { return g.getPrice() <= (Double) d; }); Criterion critErin = CFactory.makeCriterion("Kind", Kind.ELECTRIC). and(CFactory.makeCriterion("Trademark", Trademark.FENDER)). and(CFactory.makeCriterion("Model", "Stratocastor")). and(CFactory.makeCriterion("Price<=", Double.valueOf(1500))); List matchingGuitarsErin = search3(critErin);