0

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 :)

false
  • 10,264
  • 13
  • 101
  • 209
funlive
  • 61
  • 3
  • 10
  • It's too fast to measure in this tiny example, so how can you tell it's not efficient? Besides, which Prolog dialect is this? ECLiPSe? – Fred Foo Apr 14 '11 at 22:11
  • Yes, eclipse clp, I write this code by logical OOP, I don't understand well logical of prolog, so, i can't confirm :( – funlive Apr 16 '11 at 10:06

1 Answers1

2

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)).
  • which prolog do you use ? it doesn't work at eclipse clp and SWI-Prolog, thanks :) – funlive Apr 16 '11 at 10:13
  • This is tested working with SWI-Prolog using CLP(FD). I used `X in 1..10, not_between(X,3,6)` to test the predicate, and the domain of X is indeed partitioned into the sets defined by `[1,2]` and `[7,..,10]`. –  Apr 17 '11 at 03:28