prolog cut 需要建议吗?

发布于 2024-10-20 02:14:47 字数 866 浏览 2 评论 0原文

在这个任务中,我有一个 Prolog 数据库,其中包含例如 边缘(1,0) 边缘(2,0) edge(1,3)

边缘表示两个点相连。

我被要求编写一个名为reach(i,j,k)的函数,其中i是起点j是终点,k是您可以使用的步数。 需要 K 来停止递归循环,例如

假设我唯一的边从 1 到 3,而我试图到达 6。那么我无法一次性从 1 到 6。所以我会寻找一个我可以到达的地方,看看我是否可以从那里到达6。我可以一次性到达的第一个地方是3,所以我会尝试从那里到达6。

我有这样做是这样的:

    %% Can you get there in one step (need two rules because all links are
%% from smaller to greater, but we may need to get from greater to smaller.

reach1(I, J,_K) :-
    edge(I, J).
reach1(I, J,_K) :-
    edge(J, I).
%% Chhose somewhere you can get to in one step: can you get from there
%% to your target?
reach1(I,J,K) :-
    K>1,
    edge(I, B),
    K1 is K-1,
    reach1(B,J,K1).

reach1(I,J,K) :-
    K>1,
    edge(B, I),
    K1 is K-1, 
    reach1(B,J,K1).

这是有效的,但是我坚持第二部分,其中要求我们不要使用 k 而是使用“cut”来执行此操作。

有谁知道该怎么做或者可以给我一些指示吗?

in this task i have a Prolog database filled with e.g.
edge(1,0)
edge(2,0)
edge(1,3)

an edge signifies that two points are joined.

I am asked to write a function called reach(i,j,k) where i is the start point j is the end point and k is the number of steps you may use.
K is needed to stop the recursion looping e.g.

Suppose the only edge I’ve got goes from 1 to3,and I’m trying to get to 6. Then I can’t get from 1to6 in one go. so I’ll look for somewhere I can get to, and see if I can get from there to 6. The first place I can get to in one go is 3, so I’ll try to get from there to 6.

i have done this as so:

    %% Can you get there in one step (need two rules because all links are
%% from smaller to greater, but we may need to get from greater to smaller.

reach1(I, J,_K) :-
    edge(I, J).
reach1(I, J,_K) :-
    edge(J, I).
%% Chhose somewhere you can get to in one step: can you get from there
%% to your target?
reach1(I,J,K) :-
    K>1,
    edge(I, B),
    K1 is K-1,
    reach1(B,J,K1).

reach1(I,J,K) :-
    K>1,
    edge(B, I),
    K1 is K-1, 
    reach1(B,J,K1).

this works, however i am stuck with the second part in which we are asked not to use k but to use a "cut" to do this.

does anyone know how to do this or can give me some pointers?

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

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

发布评论

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

评论(1

撧情箌佬 2024-10-27 02:14:47

这种削减确保一旦以一种方式解决目标,就不会寻找另一种方式。

例如:

reach(I, J,_K) :-
    edge(I, J).

no cut - 如果由于某种原因 Prolog 回溯,它将尝试以另一种方式从 I 到达 J。
您可能会觉得,如果简单的边缘有效,则没有必要以另一种方式到达此节点,在这种情况下,您可以这样做:

reach(I, J,_K) :-
    edge(I, J),
    !.

“切断”此目标的任何替代方案,但 Prolog 已经找到了一个。

The cut ensures that once a goal has been resolved in one way, it doesn't look for another way.

example:

reach(I, J,_K) :-
    edge(I, J).

no cut - if for some reason Prolog backtracks, it will try to reach from I to J another way.
You might feel there's no point reaching this node another way if the simple edge works, and in that case you can do:

reach(I, J,_K) :-
    edge(I, J),
    !.

which "cuts" any alternative to this goal, but the one Prolog has found.

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