简体   繁体   English

prolog列出了每个可能的递归路径

[英]prolog list out every possible path of the recursion

I would like to list out every possible path 我想列出所有可能的路径

country(england,france). 
country(france,bulgaria).
country(bulgaria,germany).
country(england,bulgaria).
country(germany,italy).
edit: additional to

country(germany,italy).
country(england,italy).
country(england,greece).
country(greece,france).

connectto(X, Y) :-
country(X, Y).

?-op(150,xfy,to). ?-OP(150,XFY,向)。

X to Y:-get_waypoints(X,Y,Waypoints),write(Waypoints),fail. X到Y:-get_waypoints(X,Y,Waypoints),写(Waypoints),失败。

get_waypoints(Start, End, [Waypoint|Result]) :-
   country(Start, End),
   !;country(Start, Waypoint),
   get_waypoints(Waypoint, End, Result).

otherwise from the original code the system will give out 否则,系统会给出原始代码

   | ?- england to italy.
   []no

from the code you mention. 从你提到的代码。

now the problem comes into 现在问题出现了

    | ?- england to italy.
    [_31242|_31243][france,bulgaria,germany,_31332|_31333]   
    [bulgaria,germany,_31422|_31423]
    [greece,france,bulgaria,germany,_31602|_31603]no

although it shows out all possible route. 虽然它显示了所有可能的路线。

Any solution will be appreciated. 任何解决方案将不胜感激。

Ask if you need explanation : 询问您是否需要解释:

country(bulgaria,germany).
country(england,bulgaria).
country(england,france). 
country(england,greece).
country(england,italy).
country(france,bulgaria).
country(greece,france).
country(germany,italy).

:- op(150, xfy, to).

X to Y :-
    findall(Waypoint, get_waypoints(X,Y,Waypoint), Waypoints),
    write(Waypoints).

get_waypoints(Start, End, []) :- 
    country(Start, End).
get_waypoints(Start, End, [Waypoint|Result]) :-
    country(Start, Waypoint),
    get_waypoints(Waypoint, End, Result).

Use is : 用途是:

?- england to italy.

Here, I updated my code to match your expectations. 在这里,我更新了我的代码以符合您的期望。

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

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