prolog - 错误 1,回溯堆栈已满 [英] prolog - Error 1, Backtrack Stack Full

查看:60
本文介绍了prolog - 错误 1,回溯堆栈已满的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 prolog 中编写一个程序,以确定是否有从一个地方到另一个地方的方法.这些是关系:

im trying to write a program in prolog that determine if there is a way from place to place. these are the relations:

road(ny,florida).
road(washington,florida).
road(washington,texas).
road(vegas,california).

我想写:there_is_way(X,Y) 确定是否有办法.例如:

I want to write: there_is_way(X,Y) that determine if there is a way or not. for example:

?- road(florida,ny).
no
?-there_is_way(florida,ny).
yes

这是我的代码:

there_is_way(X,Y):- road(X,Y);
                        road(Y,X);
                        road(X,Z),road(Z,Y),X\=Y;
                        road(X,Z),road(Y,Z),X\=Y;
                        road(Z,X),road(Z,Y),X\=Y;
                        road(Z,X),road(Y,Z),X\=Y.
there_is_way(X,Y):-
                        road(Z,Y),there_is_way(X,Z).
there_is_way(X,Y):-
                        road(Y,Z),there_is_way(X,Z).

但不幸的是我收到错误 1,回溯堆栈已满".

but unfortunately I get "Error 1, Backtrack Stack Full".

有人吗?

谢谢

推荐答案

首先,我们需要一个对称定义:

First, we need a symmetric definition:

:- meta_predicate symm(2, ?,  ?).

symm(P_2, A, B) :-
   call(P_2, A, B).
symm(P_2, A, B) :-
   call(P_2, B, A).

现在,使用 closure0/3

there_is_way(A, B) :-
   closure0(symm(road), A, B).

这篇关于prolog - 错误 1,回溯堆栈已满的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆