Reverse no duplicate

Description

Reversing with no duplicates

Source: The Art of Prolog

Download

Program source code: reverse_no_duplicate.pl

Listing

/*
     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

Comments

pl/prolog/pllib/reverse_no_duplicate.txt · ostatnio zmienione: 2017/07/17 08:08 (edycja zewnętrzna)
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0