这是GNU-Prolog的谓词以配合没有重复的整数列表(所有的可能性),具有长度N,和是1域内到N
我无法得到一定的谓词上班。它的功能是它匹配一个整数列表 ,其域名1到N
没有重复,长度为N
。基本上我想要做的是有这个作为输入和输出:
| ?- row_valid(X, 3).
X = [1, 2, 3] ? ;
X = [1, 3, 2] ? ;
X = [2, 1, 3] ? ;
X = [2, 3, 1] ? ;
X = [3, 1, 2] ? ;
X = [3, 2, 1] ? ;
no
| ?- row_valid(X, 2).
X = [1, 2] ? ;
X = [2, 1] ? ;
no
| ?- row_valid(X, 1).
X = [1] ? ;
no
但现在,这是发生了什么事:
| ?- row_valid(X, 3).
X = [] ? ;
no
这可能是因为row_valid([], _).
谓语我在的发生代码。但是,我可以验证谓词匹配是否正确:
| ?- row_valid([1,2,3], 3).
true ?
yes
以下是定义的谓词。你有什么建议可以让我按照我想要的方式工作吗?谢谢你的时间。
% row_valid/2: matches if list of integers has domain of 1 to N and is not duplicated
% 1 - list of integers
% 2 - N
row_valid([], _).
row_valid(Row, N) :-
length(Row, N), % length
no_duplicates_within_domain(Row, 1, N),
row_valid(RestRow, N).
% no_duplicates/1: matches if list doesn't have repeat elements
% 1 - list
no_duplicates([]). % for empty list always true
no_duplicates([Element | RestElements]) :-
\+ member(Element, RestElements), % this element cannot be repeated in the list
no_duplicates(RestElements).
% within_domain/3 : matches if list integers are within a domain
% 1 - list
% 2 - min
% 3 - max
within_domain(Integers, Min, Max) :-
max_list(Integers, Max),
min_list(Integers, Min).
% no_duplicates_within_domain/3: matches if list integers are within a domain and isn't repeated
% 1 - list
% 2 - min
% 3 - max
no_duplicates_within_domain(Integers, Min, Max) :-
no_duplicates(Integers),
within_domain(Integers, Min, Max).
谢谢,我居然找到了一个解决方案已经但是这看起来像它的工作原理呢! GNU-Prolog没有'/ 3 /之间的',但它有'fd_domain/3'。 – axsuul 2010-11-01 11:28:03