-module(corD1). -compile(export_all). %************************************** % % Exercice 2 % %**************************************/ % 2.a plusProche(_, []) -> null; plusProche(_, [ E ]) -> E; plusProche(X, [ Head | Tail ]) -> P = plusProche(X, Tail), case abs(P - X) < abs(Head - X) of true -> P; false -> Head end. % 2.b doubleImpair([]) -> []; doubleImpair([Head | Tail]) -> case Head rem 2 == 1 of true -> [ 2 * Head | doubleImpair(Tail) ]; false -> [ Head | doubleImpair(Tail) ] end. % 2.c moyenne(L) -> F = fun(_, [], S, N) -> S / N; (Fun, [ Head | Tail ], S, N) -> Fun(Fun, Tail, S + Head, N + 1) end, F(F, L, 0, 0). %************************************** % % Exercice 3 % % %**************************************/ % 3.a split(_, []) -> { [], [] }; split(P, [H | T]) -> { L1, L2 } = split(P, T), case H =< P of true -> { [ H | L1 ], L2 }; false -> { L1, [ H | L2 ] } end. % 3.b quickSort([]) -> []; quickSort([ Head | Tail ]) -> { L1, L2 } = split(Head, Tail), quickSort(L1) ++ [ Head | quickSort(L2) ]. %************************************** % % % Exercice 4 % % %**************************************/ % 4.a der([]) -> 0; der(L) -> F = fun(_, [], X) -> X; (Fun, [H|T], _) -> Fun(Fun, T, H) end, F(F, L, null). % 4.b diff(L) -> fun(X) -> X - der(L) end. % 4.c subder(L) -> lists:map(diff(L), L). %************************************** % % Main pour les tests % %**************************************/ main() -> L = [ 13, 67, 22, 2, 52, 4 ], io:format("~B~n", [ plusProche(23, L) ]), io:format("~w~n", [ doubleImpair(L) ]), io:format("~w~n", [ moyenne(L) ]), io:format("~w~n", [ quickSort(L) ]), io:format("~w~n", [ der(L) ]), io:format("~w~n", [ (diff(L))(10) ]), io:format("~w~n", [ subder(L) ]).