range - Prolog: Get values from 0 to K - 1 -
i'm attempting create program given n, need q = 0...n.
so if given n = 1: q = 0. if given n = 5: q = 0, q = 1,...,q = 4
my attempt far:
values(n,q) :- values_helper(0,n,q). values_helper(n, n, q). values_helper(x,n,q) :- x0 x + 1, x0 < n, values_helper(x0,n,x0).
my logic behind increment x until reaches value of n, @ point program stops. however, i'm not getting bindings q, empty set. know i'm neglecting stop @ n - 1.
edit: fixed ambiguities description.
both of values_helper clauses don't expect. first 1 succeeds if first 2 arguments same, , doesn't impose constraints on q. want q set equal first argument, long it's smaller second argument:
values_helper(q, n, q) :- q < n.
in second clause, again don't use q anywhere. recursive call should values_helper(x0, n, q)
, giving:
values_helper(x, n, q) :- x0 x + 1, x0 < n, values_helper(x0, n, q).
these clauses give expected output:
?- values(5,q). q = 0 ? ; q = 1 ? ; q = 2 ? ; q = 3 ? ; q = 4 ? ; no
note n <= 0, terminates without finding value q, believe expected behavior.
Comments
Post a Comment