Reputation: 545
I have a list of numbers in prolog. the numbers are already sorted.I want to check any of the numbers are not repeated and different between any two numbers is greater than one.How to check it. any idea. Thank you.
?- check([1,3,4]). %expectation
false.
?- check([2,5,7,10]). %expectation
true.
Upvotes: 2
Views: 197
Reputation: 18726
Let me guess...
check/1
cares for are integers.If so, read on!
Use clpfd!
:- use_module(library(clpfd)).
As you already might have guessed, there are a billions and billions of ways to implement the predicate check/1
. In this answer we use a straight-forward, directly recursive approach:
check([]).
check([_]).
check([E0,E1|Es]) :-
E0+1 #< E1,
check([E1|Es]).
Sample queries:
?- check([1,3,4]). false. ?- check([2,5,7,10]). true ; % do not be bothered by the trailing `; false` false. % `true ; false` is equivalent to `true`
Have you noticed the binary operator (#<)/2
in above definition of check/1
?
It allows us to run general queries and get logically sound answers. Consider!
?- Xs = [1,A,B,C], check(Xs).
Xs = [1,A,B,C], A in 3..sup, A#=<B+ -2, B in 5..sup, B#=<C+ -2, C in 7..sup ;
false.
Upvotes: 2
Reputation: 58
I assume list alreadeTry it...
check([_]).
check(L):-append([],[X1,X2|T],L),X1+1<X2,check([X2|T]).
Upvotes: 2