如何在prolog中循环遍历规则集(不是列表)

时间:2013-11-08 15:59:52

标签: prolog

首先感谢您抽出宝贵时间考虑我的问题。我在prolog中写一个冒险风格的游戏,试图学习它。我写了以下代码片段:

i_am_at(melba_market_square).

exits :-
    i_am_at(X),
    path(X, Y, Z),
    write('Exits: '), nl, 
    write(Y), write(': '), write(Z), nl.

path(melba_market_square, s, melba_armory).
path(melba_market_square, n, melba_main_st_s).
path(melba_market_square, w, melba_sidra_alley_s).

我无法弄清楚/找到怎么做是让“退出”命令一次返回所有3条路径,而不是让我每次都击中空格键。任何帮助,将不胜感激。非常感谢你。

2 个答案:

答案 0 :(得分:1)

好的,所以我想通过这里发布的帮助来解决这个问题。这是我的代码:

i_am_at(melba_market_square).

exits :-
    i_am_at(X),
    path(X, Y, Z),
    write('Exits: '), nl, 
    write(Y), write(': '), write(Z), nl.

path(melba_market_square, s, melba_armory).
path(melba_market_square, n, melba_main_st_s).
path(melba_market_square, w, melba_sidra_alley_s).

zzz :- /* This implements "User"'s where_to_go rule. */
    i_am_at(X),
    where_to_go(X, IntoDirections),
    write('Exits: '), write(IntoDirections), nl.

where_to_go(From, IntoDirections) :- 
    findall(Direction, path(From, Direction, _), IntoDirections).

答案 1 :(得分:0)

我没试过,但我认为应该这样:

where_to_go(From, IntoDirections) :- findall(Direction, path(From, Direction, _), IntoDirections).

IntoDirections应列出您可以前往的所有路线。