【发布时间】:2017-06-20 19:27:56
【问题描述】:
我是 Prolog 的新手,在 swi-prolog.org 上找到了这个示例来解决数独问题。但我无法运行它。我查了一下same_length,只有same_length/2 而不是same_length/1。还有all_distinct/2 而不是all_distinct/0。 http://www.swi-prolog.org/pldoc/man?section=clpfd-sudoku
这是我的错误:
ERROR: d:/.../prolog/sudoku.pl:5:10: Syntax error: Operator expected
% d:/.../prolog/sudoku compiled 0.00 sec, 0 clauses
Warning: The predicates below are not defined. If these are defined
Warning: at runtime using assert/1, use :- dynamic Name/Arity.
Warning:
Warning: all_distinct/1, which is referenced by
Warning: d:/.../prolog/sudoku.pl:16:8: 2-nd clause of blocks/3
这里是 SWI-Prolog 示例的代码:
use_module(library(clpfd)).
sudoku(Rows) :-
length(Rows, 9), maplist(same_length(Rows), Rows),
append(Rows, Vs),
Vs in 1..9,
maplist(all_distinct, Rows),
transpose(Rows, Columns),
maplist(all_distinct, Columns),
Rows = [As,Bs,Cs,Ds,Es,Fs,Gs,Hs,Is],
blocks(As, Bs, Cs),
blocks(Ds, Es, Fs),
blocks(Gs, Hs, Is).
blocks([], [], []).
blocks([N1,N2,N3|Ns1], [N4,N5,N6|Ns2], [N7,N8,N9|Ns3]) :-
all_distinct([N1,N2,N3,N4,N5,N6,N7,N8,N9]),
blocks(Ns1, Ns2, Ns3).
problem(1, [[_,_,_,_,_,_,_,_,_],
[_,_,_,_,_,3,_,8,5],
[_,_,1,_,2,_,_,_,_],
[_,_,_,5,_,7,_,_,_],
[_,_,4,_,_,_,1,_,_],
[_,9,_,_,_,_,_,_,_],
[5,_,_,_,_,_,_,7,3],
[_,_,2,_,1,_,_,_,_],
[_,_,_,_,4,_,_,_,9]]).
希望你能帮我找出我的错误。
【问题讨论】:
-
此代码的链接在哪里?我对
maplist(same_length(Rows, Rows), Rows)的含义感到困惑。乍一看,其余部分在我看来并不疯狂。 -
我已将其添加到说明中
-
我自己从网站上复制并粘贴了代码,它可以工作。在您上次的编辑中,您删除了我在之前评论中指出的错字;我建议您需要再次尝试复制粘贴代码并根据链接页面运行查询,它会为您工作。
-
我使用 swi Prolog 并在 sudoku.pl 中将其 1 对 1 复制并查阅此文件。但我仍然得到错误
-
够有趣的。不,它有效。但我一点都没变
标签: prolog sudoku swi-prolog constraint-programming clpfd