Prolog - 递归添加到列表

发布于 2024-12-21 22:49:31 字数 989 浏览 1 评论 0 原文

我正在尝试编写一条规则,该规则遍历事实数据库并将每个事实的数字相加并将其保存到列表中:具体来说,这就是问题:

根据问题 5 的答案编写一条规则返回/显示两个车站之间的旅程需要多长时间。

问题 5 的答案:

time(Station1,Station2) :- overground(Station1,Station2,Time),
    overground(Station1,_,Time),
    overground(_,Station1,Time).
time(Station2,Station1) :- overground(Station2,Station1,Time),
    overground(Station2,_,Time),
    overground(_,Station2,Time).
time(Station1,Station2) :- overground(Station1,Station3,Time),
    time(Station3,Station2);
    overground(Station1,Station3,Time),
    time(Station2,Station3).
time(Station1,Station2) :- overground(Station1,_,Time),
    overground(_,Station2,Time).
time(Station1,Station2) :- overground(Station2,_,Time),
    overground(_,Station1,Time).

我尝试添加一个列表并在其后面附加“时间”,但没有成功。

*是的,它是一个分号。

还有另一个附带的文件,其中包含所有地上车站和时间,格式为“地上(X,Y,Z)”。即地上(kenton、southkenton、2)。它显示了 station1、station2 以及从 1 到 2 所需的时间。

我正在尝试遍历整个数据库,找到 X 和 Y(问题 5 所做的),然后将所有 Z 之间的值相加。两个电台并将它们放入列表中。

I'm trying to write a rule that goes through a database of facts and adds up the numbers from each fact together and saves it into a list: to be specific, this is the question:

Write a rule based on the answer to Q5 that returns/displays how long a journey will take between two stations.

Answer to question 5:

time(Station1,Station2) :- overground(Station1,Station2,Time),
    overground(Station1,_,Time),
    overground(_,Station1,Time).
time(Station2,Station1) :- overground(Station2,Station1,Time),
    overground(Station2,_,Time),
    overground(_,Station2,Time).
time(Station1,Station2) :- overground(Station1,Station3,Time),
    time(Station3,Station2);
    overground(Station1,Station3,Time),
    time(Station2,Station3).
time(Station1,Station2) :- overground(Station1,_,Time),
    overground(_,Station2,Time).
time(Station1,Station2) :- overground(Station2,_,Time),
    overground(_,Station1,Time).

I have tried adding a list and appending it with 'Time', but no luck.

*yes its a semi-colon.

There's another file that goes with it which has all of the overground stations and times in the format "overground(X,Y,Z)." i.e. overground(kenton,southkenton,2). which shows station1, station2, and the time it takes to get from 1 to 2.

I'm trying to go through the entire database, finding X and Y (which question 5 does) then adding up all of the Z's in-between the two stations and putting them into a list.

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

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

发布评论

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

评论(1

很酷又爱笑 2024-12-28 22:49:31

好吧,说实话,你的问题 5 的答案需要一些重构。首先,我将向您提供一些链接关于 href="https://stackoverflow.com/questions/8284535/can-i-keep-track-of-value-im-getting-in-an-other-variable-while-working-in-a-re/8284785 #8284785">相同相同主题

一旦您弄清楚如何处理此类递归,您当前问题的答案应该很容易。

Well, to be honest your question 5 answer needs some refactoring. I'll start by giving you some links to questions about the same subject.

Once you'll have figured out how to handle such a recursion, the answer to your current question should be easy.

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