Understanding the prologue [lists]
I have 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 a special case at the end).
This is supposedly the solution. I don't really understand how this works, can 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 would do it just like this:
pLeap(L,H,X,Y) :-
X >= L,
X =< H,
Y is X+1.
Why isn't it working (ignoring the special case at the end)?
a source to share
Operators >=
and =<
do not create their own arguments, and you can use them only after the arguments have already been created.
In other words, in this solution, X
and are Y
given values with is
, and operators <
and =<
are used only on L
and H
, whose values are set by the user. (Try this solution pLeap(L,H,2,3)
and you get the same problem as yours.)
In your case, you are trying to use >=
and =<
on X
, which doesn't matter yet, and that's why the interpreter is complaining.
a source to share