I want to create a rule in prolog that checks if there\'s a repeated number in a list.
For example:
[1,2,3,4]
it will return tru
The simplest way to check that all list members are unique is to sort list and check that length of the sorted list is equal of length of the original list.
different(X) :-
sort(X, Sorted),
length(X, OriginalLength),
length(Sorted, SortedLength),
OriginalLength == SortedLength.
Your solution doesn't work because of wrong syntax (facts and predicates should not begin with a capital letter) and a logic error. List is unique if head H
is not a member of a tail T
of a list and tail T
is unique:
different([]).
different([H|T]):-
\+member(H,T),
different(T).