prolog - How to make a list of integers from 1 to N -
i'm using gnu prolog, , told can solved finite domain solver, or member/2
, is/2
. can come using both together, it's not working. here have far:
unique([],n,0). unique([h|t],n,n1) :- length([h|t],n1), n2 n1-1, unique(t,n,n2), h #> 0, h #=< n. \+ member(h,t).
when debug unique(x,3,3)
trace
, can see when calls member/2
, it's comparing 2 domains, 1..3
, 1..3
, succeed, shouldn't. can help?
sorry solution doesn't use fd or member/2
. ;)
using prior solution problem of generating list of integers 1 n:
n_ups(n, xs) :- length(xs, n), numbered_from(xs, 1). numbered_from([], _). numbered_from([i0|is], i0) :- i1 i0+1, numbered_from(is, i1).
you can use gnu built-in permutation predicate:
unique(n, p) :- n_ups(n, l), permutation(l, p).
results unique(3, l)
:
| ?- unique(3, l). l = [1,2,3] ? l = [1,3,2] l = [2,1,3] l = [2,3,1] l = [3,1,2] l = [3,2,1] no | ?-
if want roll own permutation, then:
perm([], []). perm(list, [h|perm]) :- select(h, list, rest), perm(rest, perm).
Comments
Post a Comment