简体   繁体   English

Prolog'超出本地堆栈'

[英]Prolog 'Out of Local Stack'

I am developing a program that solves a more complicated version of the infamous puzzle 'The farmer, the fox, the goose, and the grain', which has eight components instead of four. 我正在开发一个程序,以解决臭名昭著的难题“农夫,狐狸,鹅和谷物”的更复杂版本,该难题由八个组成部分而不是四个组成部分。 I've already determined the solution; 我已经确定了解决方案; additionally, I've written out just the necessary states to complete the problem, like this: 此外,我只写出了完成问题所需的状态,如下所示:

move([w,w,w,w,w,w,w,w],[e,w,w,e,w,w,w,w]).
move([e,w,w,e,w,w,w,w],[w,w,w,e,w,w,w,w]).

etc. 等等

My goal now is to have this program follow those states, chaining from one to the next, until it reaches the ultimate goal of [e,e,e,e,e,e,e,e] . 我现在的目标是使该程序遵循那些状态,从一个状态链接到另一个状态,直到达到[e,e,e,e,e,e,e,e]的最终目标。 To accomplish this, I have defined predicates as such: 为此,我将谓词定义为:

solution([e,e,e,e,e,e,e,e],[]).
solution(Start,End) :-
    move(Start,NextConfig),
    solution(NextConfig,End).

My query is solution([w,w,w,w,w,w,w,w],[e,e,e,e,e,e,e,e]). 我的查询是solution([w,w,w,w,w,w,w,w],[e,e,e,e,e,e,e,e]). However, this results in apparently infinite recursion. 但是,这显然导致无限递归。 What am I missing? 我想念什么?

To avoid cycles, try closure0/3 为避免循环,请尝试使用closure0/3

solution(S) :-
   closure0(move, S,[e,e,e,e,e,e,e,e]).

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

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