Commit 23dc6f32 authored by Paul Lesur's avatar 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=<Y,bubble(Y,T,NT,Max). bubble(X,[Y|T],[X|NT],Max):-X=<Y,bubble(Y,T,NT,Max).
``` ```
### flatten
```prolog
flatten([[X|S]|T], F) :- flatten([X|[S|T]], F).
flatten([[]|S], F) :- flatten(S, F).
flatten([X|S], [X|T]) :- \+(X = []), \+(X = [_|_]), flatten(S, T).
flatten([], []).
```
### enleve
```prolog
retire(_,[],[]).
retire(T, [T|Q], Q).
retire(X, [T|Q], [T|NewListe]):-retire(X, Q, NewListe).
```
### ajoute
```prolog
ajoute(X,[],[X]).
ajoute(X,L,L2):-concat(L,[X],L2).
```
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment