funlive
funlive

Reputation: 61

How to write well predicate(not between)

I write code follwing

testb :-
    X::1..10,
    V1 = 3,
    V2 = 6,
    testbb(X,V1,V2),
    writeln(X).

testbb(X,V1,V2) :-
    (
      count(I,V1,V2),param(X,V1,V2) do 
      X#\=I 
    ).


?- testb.
Yes (0.00s cpu)
_385{[1, 2, 7 .. 10]}

It works well, but i think it isn't efficient

Thanks very much :)

Upvotes: 0

Views: 417

Answers (1)

user206428
user206428

Reputation:

You could confine the domain of X to lie outside the range within V1 to V2 by:

not_between(X, Lower, Upper) :-
     % it is not the case that X is both greater and 
     % equal to Lower, and less than or equal to Upper: 
    #\ ((X #>= Lower) #/\ (X #=< Upper)).

Replace your testbb/3 with not_between/3. This definition ensures that X cannot take on Lower and Upper values exactly; you could use the range constraints #< and #> instead if you wish them to be included in the domain for X.

This is tested and working with SWI-Prolog. To use CLP(FD) in a SWI-Prolog file, make sure you import the CLP(FD) library at the top of your source file in a directive, like this:

:- use_module(library(clpfd)).

Upvotes: 2

Related Questions