prolog - generate a range of ints -- "out of local stack" [beginner] -
gen(n,r)
:
r
value between 0 , n-1, in order. n
non-zero positive int. n
given.
for example: ?- genn(2,r)
. gives r=0;r=1.
implemented this, has "out of local static error":
gen(x,0). gen(x,r) :- gen(x,r1), r r1+1, r<x, % why line r>=0. % , line can't keep range successfully?
result:
?- genn2(3,r). r = 0 ; r = 1 ; r = 2 ; error: out of local stack
to understand why program not terminate, use failure-slice. end, insert goals false
understand why goals added irrelevant. if resulting fragment not terminate, original program not terminate either. can see, there not happening in part. in fact program terminate never.
gen(_x,0) :- false. gen(x,r) :- gen(x,r1), false,r r1+1,r<x,r>=0.
(there more issues: definition true goal gen(-1,0)
not intended.)
the best way solve @ once use clpfd instead of more complex handle (is)/2
or use between/3
:
gen(n0, r) :- n1 n0-1, between(0, n1, r).
Comments
Post a Comment