Prolog 路由例程

发布于 2024-08-14 17:13:26 字数 492 浏览 2 评论 0原文

我正在尝试编写一个路由函数,但似乎无法获得所需的结果。这是到目前为止的代码。 前驱找到链接到 N 的节点并将其作为 P 返回。

traceroute(_,L) :- member((A,A),L).
traceroute(N,L) :-
   predecessor(N,P),
   append(N,L,L1),
   traceroute(P,L1).

当我运行 traceroute(placeA, Y). 时,它返回此数据。 Y = [ (_G575, _G575)|_G579] .

基本上,对于跟踪路由的第一行,如果任何成员是其自身的前任,我将尝试终止递归。第二部分应该循环遍历所有节点并将它们添加到列表(L)中。

节点存储为 [(placeA,placeB),(placeB,placeC)] ,列表应存储为 [placeA,placeB,placeC]

我不明白为什么会得到这些结果。

I am trying to write a routing function but I cannot seem to get the result needed. This is the code so far.
Predecessor finds the node that is linked to N and returns it as P.

traceroute(_,L) :- member((A,A),L).
traceroute(N,L) :-
   predecessor(N,P),
   append(N,L,L1),
   traceroute(P,L1).

When I run my traceroute(placeA, Y). it returns this data..
Y = [ (_G575, _G575)|_G579] .

Basically for the first line of traceroute I am trying to terminate the recursion if any member is a predecessor of itself. The second part should be loop through all the nodes and add them to the list (L).

The nodes are stored like [(placeA,placeB),(placeB,placeC)] and the list should store like [placeA,placeB,placeC]

I can't understand why I am getting these results.

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

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

发布评论

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

评论(1

我的黑色迷你裙 2024-08-21 17:13:26

看起来像这样的解决方案(当它不正确时)通常意味着您没有在您应该拥有的地方接地(即实例化)一个术语。

我不完全确定您的代码是如何工作的,但看起来您的主要问题是您为traceroute 的第二个参数传递了一个非接地变量。

在成员调用中,由于 L 是非接地的,因此您实际上要求 prolog 返回 (A,A) 形式的项目,它是完全未实例化的列表的元素。虽然这没有多大意义,但有时做这种事情是有用的,因此 prolog 尽职尽责地遵守并会(在回溯时)生成非地面长度不断增加的列表(因为您没有在任何地方指定长度)在某个时刻具有项目 (A,A) 的变量。即:

?- traceroute(placeA, Y).
Y = [ (_G271, _G271)|_G275] ;
Y = [_G274, (_G271, _G271)|_G278] ;
Y = [_G274, _G277, (_G271, _G271)|_G281] ;
Y = [_G274, _G277, _G280, (_G271, _G271)|_G284] ;

您需要传递一个基本的 Y 值,或者进一步限制它在谓词中采用的值,甚至可能两者都限制。

此外,即使第二个子句实际上并未执行,您也会遇到类似的问题:将再次为非接地的 L 附加到 N 上以产生 L1,然后该 L1 也为非接地。由于该谓词递归地执行此操作,因此最终列表始终是非基础的。

A solution that looks like this (when it's incorrect) usually means that you haven't ground (ie instantiated) a term somewhere where you should have.

I'm not completely sure how your code is meant to work, but it looks like your main trouble is that you're passing a nonground variable for the second argument of traceroute.

In the member call, since L is nonground, you're actually asking prolog to return an item of the form (A,A) which is an element of a completely uninstantiated list. While this doesn't make all that much sense there are times when doing this sort of thing is useful, so prolog dutifully complies and will (upon backtracking) generate lists of increasing length (since you haven't specified the length anywhere) of nonground variables that has an item (A,A) at some point. ie:

?- traceroute(placeA, Y).
Y = [ (_G271, _G271)|_G275] ;
Y = [_G274, (_G271, _G271)|_G278] ;
Y = [_G274, _G277, (_G271, _G271)|_G281] ;
Y = [_G274, _G277, _G280, (_G271, _G271)|_G284] ;

You need to either pass a value for Y which is ground or further constrain the values that it takes inside your predicate or possibly even both.

Also even though the second clause isn't actually executed, you have a similar problem there: L which is again nonground is appended onto N to yield L1 which is then also nonground. Since this predicate does this recursively, the final list is always always going to be nonground.

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