简体   繁体   English

在Prolog中创建列表列表

[英]Creating a list of lists in Prolog

I want to have a list of lists with constraints, here's my code written in SWI-Prolog: 我想要一个带有约束的列表列表,这是我用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.

But, it gives ERROR:Type Error: integer expected . 但是,它给出了ERROR:Type Error: integer expected

Using library CLP(FD) in SWI-Prolog 7.1.11 I get: 在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.

Maybe the error was due to the dot instead of a comma at the end of the first line (corrected now)? 也许错误是由于第一行末尾的点而不是逗号引起的(现在已更正)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM