《归来》 Prolog 中谓词的列表

发布于 2024-09-26 15:09:00 字数 480 浏览 4 评论 0原文

resolve(K, K, _) :- writeln('finished'). %goal state

resolve(CurrentState, GoalState, Path) :-
    suc(_, CurrentState, NextState, GoalState),
    append(Path, [CurrentState], NextPath),
    resolve(NextState, GoalState, NewPath).

我目前有这个算法,它可以正常工作。我像这样运行它:

resolve(0, 10, Path).

我确信算法正在按其应有的方式运行,它会达到目标状态,尽管 Path 的值

Path = []

不是应该发生的情况。路径应包含我的算法已通过的“状态”序列。 可能是什么问题?

resolve(K, K, _) :- writeln('finished'). %goal state

resolve(CurrentState, GoalState, Path) :-
    suc(_, CurrentState, NextState, GoalState),
    append(Path, [CurrentState], NextPath),
    resolve(NextState, GoalState, NewPath).

I currently have this algorithm, and it works as it should. I am running it like this:

resolve(0, 10, Path).

I know for sure that the algorithm is running as it should, it gets to the goal state, although Path's value is

Path = []

which is not what should happen. Path should contain the sequence of "states" in which my algorithm has passed.
What might be the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晚风撩人 2024-10-03 15:09:01

使用 DCG 表示法来描述列表是最简单的:

path(State0, Target) -->
    (    { State0 == Target } -> []
    ;    { suc(_, State0, State1, Target) },
         [State1],
         path(State1, Target)
    ).

您也可以手动完成:

path(State0, Target, Path) :-
    (    State0 == Target -> Path = []
    ;    suc(_, State0, State1, Target),
         Path = [State1|Rest],
         path(State1, Target, Rest)
    ).

这里不需要累加器来获取线性时间。

It's easiest to use DCG notation to describe the list:

path(State0, Target) -->
    (    { State0 == Target } -> []
    ;    { suc(_, State0, State1, Target) },
         [State1],
         path(State1, Target)
    ).

You can also do it manually:

path(State0, Target, Path) :-
    (    State0 == Target -> Path = []
    ;    suc(_, State0, State1, Target),
         Path = [State1|Rest],
         path(State1, Target, Rest)
    ).

There is no need for accumulators here to get linear time.

淡写薰衣草的香 2024-10-03 15:09:01

我相信您构建路径的方式存在问题。
您可能想要重写它,以便将其构建在谓词的头部。
像这样的事情:

resolve(K, K, []) :- writeln('finished'). %goal state
resolve(CurrentState, GoalState, [CurrentState|Path]) :-
    suc(_, CurrentState, NextState, GoalState),
    resolve(NextState, GoalState, Path).

第一个子句结束递归:要从状态 K 转到状态 K,您返回 [] 作为路径,因为您已经处于目标状态。
第二个子句构建路径,它获取下一个状态并递归调用resolve,在递归完成时构建您遍历的路径。

I believe there is a problem in the way you want to build the Path.
You might want to rewrite it so as to build it in the head of your predicate.
Something like this:

resolve(K, K, []) :- writeln('finished'). %goal state
resolve(CurrentState, GoalState, [CurrentState|Path]) :-
    suc(_, CurrentState, NextState, GoalState),
    resolve(NextState, GoalState, Path).

The first clause ends the recursion: to go from state K to state K you return [] as the Path as you are already in the Goal state.
The second clause builds the path, it gets the next state and calls recursively resolve, building the Path you have traversed when the recursion finishes.

夜光 2024-10-03 15:09:01

append 谓词中的术语 NextPath 应该是 NewPath 吗?

目前 NextPath 没有任何其他用途,因此 Path 必须绑定到 [],因为 NextPath 能够完全绑定到[CurrentState]

Should the term NextPath in your append predicate be NewPath?

Currently there isn't any other usage of NextPath so Path must be binding to [] because NextPath is able to bind fully to [CurrentState].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文