Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Prolog Remove Duplicates

Status
Not open for further replies.

Krytikal

Programmer
Joined
Mar 31, 2009
Messages
1
Location
CA
I am very new to prolog and I was wondering if am on the right approach to my programs specifications. I need to remove the duplicates from a list.

remove_duplicates([],_).
remove_duplicates([X|XS], Y) :-
member(X,Y),
remove_duplicates(XS,Y), !.
remove_duplicates([X|XS], Y) :-
\+member(X,Y),
remove_duplicates(XS,[Y|X]), !.

I have this so far and it works but I was wondering how I can use my program to confirm stuff like:

remove_duplicates([5,4,4,2,2], [5,4,2]).
and it will return true.

Thank you
 
You have two problems :
remove_duplicates([],_). You attach a free variable with an empty list this is why remove_duplicates([5,4,4,2,2], L). gives you [5, 4, 2 | GXXX].
You have also an error here remove_duplicates(XS,[Y|X]), !.
It should be remove_duplicates(XS,[X|Y]),but tou get [2, 4, 5] or append(Y, [X], Y1) and remove_duplicates(XS,[Y1),

The error is not seen because of your first mistake remove_duplicates([],_).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top