projog

3.45. limit(N, X) - calls the goal represented by a term a maximum number of times.

Evaluates the goal represented by X for a maximum of N attempts.

Examples

?- limit(3, repeat).

yes;

yes;

yes

?- limit(-1, true).

no

?- limit(0, true).

no

?- limit(1, true).

yes

?- limit(2, true).

yes

?- limit(-1, fail).

no

?- limit(0, fail).

no

?- limit(1, fail).

no

?- limit(2, fail).

no

?- assert(p(a)), assert(p(b)), assert(p(c)).

yes

?- limit(-1, p(X)).

no

?- limit(0, p(X)).

no

?- limit(1, p(X)).
X = a

yes

?- limit(2, p(X)).
X = a

yes;
X = b

yes

?- limit(3, p(X)).
X = a

yes;
X = b

yes;
X = c

yes

?- limit(4, p(X)).
X = a

yes;
X = b

yes;
X = c

yes

?- X=a, limit(3, p(X)).
X = a

yes

?- X=b, limit(3, p(X)).
X = b

yes

?- X=c, limit(3, p(X)).
X = c

yes

?- X=d, limit(3, p(X)).

no

?- X=fail, limit(3, X).

no

?- X=true, limit(3, X).
X = true

yes

?- X=repeat, limit(3, X).
X = repeat

yes;
X = repeat

yes;
X = repeat

yes

?- limit(a, true).

Expected Numeric but got: ATOM with value: a

?- limit(X, true).

Expected Numeric but got: VARIABLE with value: X

?- limit(3, X).

Expected an atom or a predicate but got a VARIABLE with value: X

?- limit(3, 999).

Expected an atom or a predicate but got a INTEGER with value: 999

?- limit(3, unknown_predicate).

no

p(a,1).
p(a,2).
p(a,3).
p(a,4).
p(a,5).
p(b,1).
p(b,2).
p(b,3).
p(c,1).
p(c,3).
p(d(1),1).
p(d(2),2).
p(d(3),2).
p(d(4),2).
p(d(5),5).

?- limit(3, p(a,X)).
X = 1

yes;
X = 2

yes;
X = 3

yes

?- limit(6, p(X,2)).
X = a

yes;
X = b

yes;
X = d(2)

yes;
X = d(3)

yes;
X = d(4)

yes

?- limit(7, p(d(X),X)).
X = 1

yes;
X = 2

yes;
X = 5

yes