List duplicates remove

Description

Removing duplicates from a list.

Source: The Art of Prolog

Download

Program source code: list_duplicates_remove.pl

Listing

/*
	no_doubles(Xs,Ys) :-
		Ys is the list obtained by removing 
		duplicate elements from the list Xs.
*/
 
no_doubles([X|Xs],Ys) :-
	member(X,Xs), no_doubles(Xs,Ys).
no_doubles([X|Xs],[X|Ys]) :-
	nonmember(X,Xs), no_doubles(Xs,Ys).
no_doubles([],[]).
 
     nonmember(X,[Y|Ys]) :- X \== Y, nonmember(X,Ys).
     nonmember(X,[]).
 
%  Program 7.9   Removing duplicates from a list

Comments

pl/prolog/pllib/list_duplicates_remove.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