citxx
citxx

Reputation: 2545

How to get a list of possible predicate values

Lets I have a predicate p/1 defined, for example, as follows:

p(2).
p(3).
p(5).
p(7).

How can I define a predicate p_list/1 which will be true for a list of all possible values of p/1 (in above case - [2, 3, 5, 7]) in the backtracking order?

Simple enumeration of the values is not acceptable because it makes maintenance more difficult. Moreover values, can be defined implicitly.

Upvotes: 2

Views: 1415

Answers (2)

CapelliC
CapelliC

Reputation: 60004

maplist/2 works well for check, while findall/3 it's the basic list constructor in Prolog

Try

?- findall(X, p(X), L), maplist(p, L).

Upvotes: 2

joel76
joel76

Reputation: 5615

You can use bagof(X, p(X), L) which gives you L = [2,3,5,7]. What do you mean by "defined implicitly" ? Can you give an example.

Upvotes: 3

Related Questions