prolog - multiple outputs, same variable -
how following program produce following output:
?- genn(3,r). r=0; r=1; r=2;
my current output r=0;
code:
genn(1,0). genn(n,r) :- n > 1, n1 n-1, genn(n1,r1), r r1.
newer code:
genn(1,r):-r 0. genn(n,r) :- n > 1, n1 n-1, genn(n1,r), r n1.
the above program meant output numbers 0 n-1.
if prolog implementation supports clpfd, write genn/2
this:
:- use_module(library(clpfd)). genn(up,n) :- n #>= 0, n #< up, indomain(n).
sample query:
?- genn(3,n). n = 0 ; n = 1 ; n = 2.
Comments
Post a Comment