James Notaro
James Notaro

Reputation: 137

list of ordered pairs in prolog

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

Upvotes: 1

Views: 790

Answers (1)

user1812457
user1812457

Reputation:

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;

Upvotes: 2

Related Questions