views:

116

answers:

2

I am to write a program that does this:

?- pLeap(2,5,X,Y).
X = 2,
Y = 3 ;
X = 3,
Y = 4 ;
X = 4,
Y = 5 ;
X = 5,
Y = 5 ;
false.

(gives all pairs X,X+1 between 2 and 5, plus the special case at the end).

This is supposedly the solution. I don't really understand how it works, could anyone guide me through it?

pLeap(X,X,X,X).
pLeap(L,H,X,Y) :-
        L<H,
        X is L,
        Y is X+1.
pLeap(L,H,X,Y) :-
        L=<H,
        L1 is L+1,
        pLeap(L1,H,X,Y).

I'd do it simply like this:

pLeap(L,H,X,Y) :-
        X >= L,
        X =< H,
        Y is X+1.

Why doesn't it work (ignoring the special case at the end)?

+1  A: 

The >= and =< operators don't instantiate their arguments, and you can only use them once the arguments have already been instantiated.

Put another way, in the given solution, X and Y are given values with is, and the < and =< operators are only used on L and H, whose values are given by the user. (On the given solution, try pLeap(L,H,2,3) and you'll get the same problem as you're having.)

In your case, though, you try to use >= and =< on X, which has no value yet, and so the interpreter complains.

Etaoin
+1  A: 

You could use library clpfd for you problem.

:- use_module(library(clpfd)).

pLeap(L,H,X,Y) :-
    X in L..H,
    Y #= min(H, X+1),
    label([X]).

Here is the output:

 ?- pLeap(2,5,X,Y).
X = 2,
Y = 3 ;
X = 3,
Y = 4 ;
X = 4,
Y = 5 ;
X = 5,
Y = 5.
Xonix