1
votes

If I have a database such as:

number(0).
number(1).
number(2).
number(3).
number(4).
number(5).
number(6).
number(7).
number(8).
number(9).

and want to write a predicate numbers(L) that instantiates that L to a list of numbers. i.e.

numbers([A,B]).

should instantiate A and B to 10*10 different combinations of numbers, how would I do this. I want to show the recursion in the numbers(L) predicate, not use maplist.

Many thanks for your assistance

1

1 Answers

1
votes

First, you should not use number/1 for your purposes, because it is a name of a built-in predicate. Rename number to num or some other name.

A rule that produces a pair of numbers is trivial:

numbers([A,B]) :- num(A), num(B).

Yes, that was really it!

Now you can print all of the combinations like this:

:- numbers([A,B]), write(A), write('-'), write(B), nl, fail.

Here is a demo on ideone.