Commit 23dc6f32 by Paul Lesur

### Ajouts : flatten, retire, ajoute

parent aa783fce
 # Fiche aide IA02 Prolog ### Member ## Member ```prolog ```prolog member(X, [X|_]). member(X, [X|_]). member(X, [_|Tail]) :-, member(X, [_|Tail]) :-, member(X, Tail). member(X, Tail). ``` ``` ## Append / Concat ### Append / Concat ```prolog ```prolog append([],X,X). append([],X,X). append([X|Y],Z,[X|W]) :- append(Y,Z,W). append([X|Y],Z,[X|W]) :- append(Y,Z,W). ``` ``` ## Reverse ### Reverse ```prolog ```prolog reverse([],[]). reverse([],[]). reverse([X|Xs],YsX) :- reverse(Xs,Ys), append(Ys,[X],YsX). reverse([X|Xs],YsX) :- reverse(Xs,Ys), append(Ys,[X],YsX). ``` ``` ## Length ### Length ```prolog ```prolog len([], 0). len([], 0). len([_ | Tail], Length) :- len([_ | Tail], Length) :- ... @@ -26,13 +25,13 @@ len([_ | Tail], Length) :- ... @@ -26,13 +25,13 @@ len([_ | Tail], Length) :- Length is Length1 + 1,!. Length is Length1 + 1,!. ``` ``` ## Nième élément ### Nième élément ```prolog ```prolog element_n(1,[X|Q],X):-!. element_n(1,[X|Q],X):-!. element_n(N,[T|Q],X):-M is N-1, element_n(M,Q,X). element_n(N,[T|Q],X):-M is N-1, element_n(M,Q,X). ``` ``` ## Tri à bulles ### Tri à bulles ```prolog ```prolog bubble_sort(List,Sorted):-b_sort(List,[],Sorted). bubble_sort(List,Sorted):-b_sort(List,[],Sorted). b_sort([],Acc,Acc). b_sort([],Acc,Acc). ... @@ -42,3 +41,21 @@ bubble(X,[],[],X). ... @@ -42,3 +41,21 @@ bubble(X,[],[],X). bubble(X,[Y|T],[Y|NT],Max):-X>Y,bubble(X,T,NT,Max). bubble(X,[Y|T],[Y|NT],Max):-X>Y,bubble(X,T,NT,Max). bubble(X,[Y|T],[X|NT],Max):-X=
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!