Różnice
Różnice między wybraną wersją a wersją aktualną.
|
|
— |
pl:prolog:pllib:reverse_no_duplicate [2019/06/27 15:50] (aktualna) |
| ====== Reverse no duplicate ====== |
| {{tag>lists}} |
| ===== Description ===== |
| Reversing with no duplicates |
| |
| **Source**: The Art of Prolog |
| ===== Download ===== |
| Program source code: {{reverse_no_duplicate.pl}} |
| ===== Listing ===== |
| <code prolog> |
| /* |
| nd_reverse(Xs,Ys) :- |
| Ys is the reversal of the list obtained by |
| removing duplicate elements from the list Xs. |
| */ |
| nd_reverse(Xs,Ys) :- nd_reverse(Xs,[],Ys). |
| |
| nd_reverse([X|Xs],Revs,Ys) :- |
| member(X,Revs), nd_reverse(Xs,Revs,Ys). |
| nd_reverse([X|Xs],Revs,Ys) :- |
| nonmember(X,Revs), nd_reverse(Xs,[X|Revs],Ys). |
| nd_reverse([],Ys,Ys). |
| |
| nonmember(X,[Y|Ys]) :- X \== Y, nonmember(X,Ys). |
| nonmember(X,[]). |
| |
| % Program 7.10 Reversing with no duplicates |
| </code> |
| ===== Comments ===== |
| |