简体   繁体   English

(序言)获取没有列表的变量总和

[英](Prolog) Get sum of variables without list

very new to Prolog and I'm absolutely awful at it. 对Prolog来说是很新的东西,我对此感到非常恐惧。

I am trying to get a sum for all the marks received on assignments, and return it. 我正在尝试获取分配给作业的所有分数的总和,并将其返回。 The sum predicate must be of the form sumAssignments(S) . 总和谓词的格式必须为sumAssignments(S)

Here's the knowledge base I've made: 这是我所做的知识库:

assignment(1, A).
assignment(2, B).
assignment(3, C).
assignment(4, D).
assignment(5, E).

...Where assignment(1, A) means that assignment 1 has a variable grade A (could be 70, could be 50, etc.). ...在assignment(1, A) ,赋值1具有可变的等级A(可以是70,可以是50等)。

Here is my attempt at getting the sum, just for the first two assignments for testing purposes: 这是我尝试获得的总和,仅用于测试的前两个任务:

sumAssignments(S) :- assignment(1, A), assignment(2, B), S=A+B.

That always returns yes. 总是返回yes。 The key here is I can't use lists. 这里的关键是我不能使用列表。

I found out that only constants belong in predicates, so instead of assignment(1, A). 我发现只有常量属于谓词,所以不是assignment(1, A). we would find an arbitrary number to represent 'A', in this case maybe 67 or 100: assignment(1, 67). 我们会找到一个任意数字来表示“ A”,在这种情况下可能是67或100: assignment(1, 67). The summation works fine after doing that. 这样做之后,求和工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM