I need to do a function in Prolog that, given a list, check if every member is different. The only particularity is that it must not count "_" variables. Here is an example of what I want :
unique([3,1,2]).
> true
unique([3,1,2,_]).
> true
unique([3,1,2,_,_,_]).
> true
unique([3,1,2,1]).
> false
unique([3,1,2,1,_]).
> false
I already tried with the following function, using the predicate "var" to check if the variable is free or not, but it doesn't work :
element(X,[X|_]) :- !.
element(X,[_|Q]) :- element(X,Q).
unique([]).
unique([_,[]]).
unique([T,Q]) :- var(T), unique(Q), !.
unique([T|Q]) :- \+element(T,Q),unique(Q).
Thank you for your help
PS: I'm using GProlog
PS2: I know that the function fd_all_different could work, but I'd rather implement it myself
unique([3,1,2,X,X]).
do? – Arndt Jonassonall_different/1
. See this. – falsedif/2
defined – falseunique([_,[]]).
==>unique([_]).
andunique([T,Q]) :- ...
==>unique([T|Q]) :- ...
. – lurker