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.
2
votes
2 Answers
2
votes
2
votes
Let me guess...
- you are using SWI-Prolog, and
- all numbers that your
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.
[X1, X2|T]
whereX1
andX2
are the first two elements in the list, andT
is the rest of the list (the tail, also a list). – lurker