1
votes

I am trying to implement a prolog method that will take a number and return a list of all possible ordered pairs where both X and Y are less than the number given. For example

genXY(2,R).

Should return

R=[0,0];
R=[0,1];
R=[1,0];
R=[1,1].

I am having trouble understanding how to implement this. I have written the code

genN(N,R) :-
    N1 is N-1,
    between(0,N1,R).

Which will give the following output when executed

genN(3,R).
R=0;
R=1;
R=2;

And I believe that I should use forall to implement genXY but I don't understand how I would go about doing this

1
What exactly means "ordered pairs up to that number"? How does that correspond to the example you have given (0-0, 0-1, 1-0, 1-1)? Do you mean "pairs X-Y so that X+Y=0..N"?user1812457
As in if i am given the number 2 I need to return every possible combination of ordered pairs where both X and Y are less then 2. Hence 0-0, 0-1, 1-0, 1-1.James Notaro
So you want the lists of N numbers between 0 et N-1. Hint : look at the lists of N numbers between 0 and P-1. How to apply recursion?Michel Billaud
I don't just what the list I want every possible combination. Also what is P-1 in this case.James Notaro

1 Answers

2
votes

Just use between/3 twice:

?- N = 2, R = X-Y, succ(N0, N), between(0, N0, X), between(0, N0, Y).

R = 0-0;
R = 0-1;
R = 1-0;
R = 1-1;