在Prolog中创建列表列表

时间:2014-04-08 22:21:47

标签: list prolog clpfd

我想要一个带有约束的列表列表,这里的代码是用SWI-Prolog编写的:

List = [L1,L2,L3],
L1 = [X1, X2], L1 ins 1..4,
L2 = [Y1, Y2], L2 ins 1..4,
L3 = [Z1, Z2], L3 ins 1..4.

但是,它提供了ERROR:Type Error: integer expected

1 个答案:

答案 0 :(得分:5)

在SWI-Prolog 7.1.11中使用库CLP(FD)我得到:

?- use_module(library(clpfd)).
%    library(pairs) compiled into pairs 0.00 sec, 22 clauses
%   library(lists) compiled into lists 0.01 sec, 122 clauses
%   library(occurs) compiled into occurs 0.00 sec, 14 clauses
%  library(apply_macros) compiled into apply_macros 0.02 sec, 175 clauses
%  library(assoc) compiled into assoc 0.01 sec, 103 clauses
% library(clpfd) compiled into clpfd 0.18 sec, 2,848 clauses
true.

?- List = [L1,L2,L3],
L1 = [X1, X2], L1 ins 1..4,
L2 = [Y1, Y2], L2 ins 1..4,
L3 = [Z1, Z2], L3 ins 1..4.
List = [[X1, X2], [Y1, Y2], [Z1, Z2]],
L1 = [X1, X2],
L2 = [Y1, Y2],
L3 = [Z1, Z2],
X1 in 1..4,
X2 in 1..4,
Y1 in 1..4,
Y2 in 1..4,
Z1 in 1..4,
Z2 in 1..4.

可能错误是由于点而不是第一行末尾的逗号(现在更正)?