-module(cor03). -compile(export_all). %************************************************** % % Correction du TP 03 % %************************************************** %--------------------------------------------- % Exo 1 %--------------------------------------------- maxL([]) -> null ; maxL([ Elem ]) -> Elem ; maxL([ Elem | Reste ]) -> max(Elem, maxL(Reste)). maxLT([]) -> null ; maxLT([ Elem | Reste ]) -> maxTerm(Reste, Elem). maxTerm([], Acc) -> Acc; maxTerm([H | T], Acc) -> case Acc < H of true -> maxTerm(T, H); false -> maxTerm(T, Acc) end. %---- maxLL([]) -> []; maxLL([ L | Reste ]) -> [ maxL(L) | maxLL(Reste) ]. %--------------------------------------------- % Exo 2 %--------------------------------------------- presence(_, []) -> false; presence(X, [ Elem | Reste ]) -> (X == Elem) orelse presence(X, Reste). %---- assoc(_, []) -> { null, notFound }; assoc(Key, [ { Key, Value } | Tail ]) -> { ok, Value }; assoc(Key, [ { _, _ } | Tail ]) -> assoc(Key, Tail). %--------------------------------------------- % Exo 3 %--------------------------------------------- distribution(L) -> distriTerm(L, { [], [] }). distriTerm([], Pair) -> Pair; distriTerm([Elem | Tail], { L1, L2 }) -> distriTerm(Tail, { L2, [Elem | L1] }). % -- d'autres solutions -- split([]) -> { [] , [] }; split([ Head1 | [] ]) -> { [ Head1 ] , [] }; split([ Head1 | [ Head2 | Tail2 ] ]) -> { L1, L2 } = split(Tail2), { [ Head1 | L1 ], [ Head2 | L2 ] }. split2([]) -> { [] , [] }; split2([ Head | Tail ]) -> { L1, L2 } = split2(Tail), { L2, [ Head | L1 ] }. %--------------------------------------------- % Exo 4 %--------------------------------------------- longueurLigne([]) -> 0; longueurLigne([ _ | [] ]) -> 0; longueurLigne( [ Point1 | [ Point2 | Reste ] ] ) -> distance(Point1, Point2) + longueurLigne([ Point2 | Reste]). distance( {X1, Y1} , {X2, Y2} ) -> math:sqrt( (X2 - X1) * (X2 - X1) + (Y2 - Y1) * (Y2 - Y1) ). %--------------------------------------------- % Exo 5 %--------------------------------------------- traitement([]) -> executer({ 0, "Stopper" }); traitement([ Head | Tail ]) -> executer(Head), traitement(Tail). executer({ _, Tache }) -> io:format("~s. Done.~n", [ Tache ]). ajout(T , []) -> [ T ]; ajout({ Priority, Task } , [ {PrioHead, TaskHead} | Tail ]) -> case Priority < PrioHead of true -> [ {PrioHead, TaskHead} | ajout({ Priority, Task }, Tail) ]; false -> [ {Priority, Task} | [ {PrioHead, TaskHead} | Tail ] ] end. %--------------------------------------------- % Main function %--------------------------------------------- main() -> maxL([ 10, 2, 20, 3, 1]), maxL( [ 2 ]), maxLL([ [ 10, 2, 20, 3, 1], [2,3, 4], [6, 19] ]), presence(3, [ 10, 2, 20, 3, 1]), presence(11, [ 10, 2, 20, 3, 1]), assoc(3, [ {10, 'a'}, {2, 'b'}, {20, 'c'}, {3, 'd'}, {1, 'e'}]), distribution(lists:seq(1, 19)), P1 = { 0, 0 }, P2 = {10, 10}, distance(P1, P2), Ligne = [ P1, P2, {5, 7} ], longueurLigne(Ligne), TasksList = ajout( { 5, "Prendre le train"} , ajout({10, "Boire un café"}, ajout({3, "Donner le cours"}, ajout( {1, "Répondre aux emails"}, [ ])))), traitement(TasksList).