正确使用 findall/3,尤其是第一个模板参数

发布于 2024-10-22 21:29:59 字数 262 浏览 8 评论 0原文

我知道prolog中有一个内置函数findall/3, 我试图找到总小时数(Thrs)并将它们存储在一个列表中,然后将列表相加。但这对我不起作用。这是我的代码:

totalLecHrs(LN,THrs) :-
    lecturer(LN,LId),
    findall(Thrs, lectureSegmentHrs(CC,LId,B,E,THrs),L),
    sumList(L,Thrs).

你能告诉我它有什么问题吗?多谢。

i know there is a build-in function findall/3 in prolog,
and im trying to find the total numbers of hours(Thrs) and store them in a list, then sum the list up. but it doesnt work for me. here is my code:

totalLecHrs(LN,THrs) :-
    lecturer(LN,LId),
    findall(Thrs, lectureSegmentHrs(CC,LId,B,E,THrs),L),
    sumList(L,Thrs).

could you tell me what's wrong with it? thanks a lot.

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

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

发布评论

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

评论(2

我只土不豪 2024-10-29 21:29:59

您需要在 findall/3 子目标中使用“虚拟”变量来表示“小时”。您编写的内容使用 THrs 作为 sumList/2 的返回值,并作为 findall/3 在 L 中列出的变量。使用 X 作为 findall 的第一个参数,并在相应的子目标 lectureSegmentHrs/5 中作为最后一个参数。

You need to use a "dummy" variable for Hours in the findall/3 subgoal. What you wrote uses THrs both as the return value for sumList/2 and as the variable to be listed in L by findall/3. Use X as the first argument of findall and in the corresponding subgoal lectureSegmentHrs/5 as the last argument.

梦行七里 2024-10-29 21:29:59

看起来问题在于您为不同的事情使用了同一个变量 (Thrs) 两次。然而,这很难说,因为您还在不同的地方使用了不同的大写字母。更改 findall 行,以便初始变量在 lectureSegmentHrs 调用中具有相同的大小写。然后使用完全不同的变量来获取最终输出值(即出现在 sumList 和整个谓词的返回槽中的值)。

您需要使用不同的变量,因为 Prolog 不支持变量重新分配。在逻辑语言中,重新分配变量的概念本质上是不可能的。像下面这样的东西可能看起来很明智......

...
X = 10,
X = 11,
...

但你必须记住 Prolog 中的 , 是合取运算符。您实际上是在告诉 Prolog 找到 X 同时为 10 和 11 的问题的解决方案。同时。所以它显然会告诉你这是不可能的。

相反,您必须在进行过程中编写新的变量名称。有时这确实有点烦人,但这只是符合逻辑语言的领域。

It looks like the problem is that you're using the same variable (Thrs) twice for different things. However it's hard to tell as you've also used different capitalisation in different places. Change the findall line so that the initial variable has the same capitalisation in the lectureSegmentHrs call. Then use a different variable completely to get the final output value (ie the one that appears in sumList and in the return slot of the entire predicate).

You need to use a different variable because Prolog does not support variable reassignment. In a logical language, the notion of reassigning a variable is inherently impossible. Something like the following may seem sensible...

...
X = 10,
X = 11,
...

But you have to remember that , in Prolog is the conjunction operator. You're effectively telling Prolog to find a solution to your problem where X is both 10 and 11 at the same time. So it's obviously going to tell you that that can't be done.

Instead you have to just make up new variable names as you go along. Sometimes this does get a bit annoying but it's just goes with the territory of a logical languages.

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