Mini sudoku solver in Prolog stops partway through

好久不见. 提交于 2019-12-04 10:07:45

It seems to me you are relying on the default display in SWI-Prolog to write the list that represents the solution, and it's a feature of SWI-Prolog that it doesn't print all the entries of a long list in this case, replacing the tail after nine items with the "ellipsis" ...

You stumbled onto this when you added write(Puzzle) to the goal and thus saw the entire list. SWI-Prolog's site has a FAQ about this "abbreviation" of lists.

You typed in the ; yourself, didn't you? ; asks for more solutions. Since you didn't use labeling on the variables, Prolog only constrains them without actually generating a complete solution (it does some constraint propagation to derive a few of the values). There's only one way to put the constraints, so there's no second solution. If you put a call to labeling at the end of the sudoku clause, you can cycle through the solutions.

(PS.: sudoku doesn't need two arguments, since you unify them with Solution = Puzzle.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!