Prolog中列表中元素的总和几乎不需要解释 [英] Sum of elements in list in Prolog needs little explanation

查看:52
本文介绍了Prolog中列表中元素的总和几乎不需要解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 prolog 编程的初学者,我希望你谦虚并帮助我解决这个困惑

I'm a beginner at prolog programming and I hope you humble and help me to pass this confusion

我在计算序言中的总和时遇到了问题,我有答案,但我不太清楚.答案是:

I'm facing a problem to calculate the sum in prolog and I have the answer but it's not so clear to me. The answer is:

list_sum([], 0).
list_sum([Head | Tail], Total) :-
   list_sum(Tail, Sum1),
   Total = Head + Sum1.

我不明白的是Sum1 是什么以及程序将如何按步骤运行

what I did not understand is what is the Sum1 and how the program will work in steps

它首先会检查第一个条件list_sum([], 0).当条件不满足时,它将把列表分成两部分Head然后?

it first will check the first condition list_sum([], 0). while the condition is not met it will divide the list into 2 parts Head and Tail then?


我希望你接受一个小初学者,给他一些时间来纠正他的困惑.
谢谢你们


I hope you accept a little beginner and give him some time to correct his confusing.
Thanks you guys

推荐答案

这是经典的递归方法 - 您需要熟悉它才能理解 Prolog.

This is the classic recursive approach - you need to get comfortable with it to understand Prolog.

您的规则有两个子句 - 一个用于空列表,另一个用于非空列表.空列表子句表示空列表的元素总和为零(这是完全合理的).这被称为递归的基本情况".每个终止递归规则都必须有一个基本情况.

Your rule has two clauses - the one for the empty list, and the one for a non-empty one. The empty list clause says that the sum of elements of an empty list is zero (which is perfectly reasonable). This is called "the base case of recursion". Every terminating recursive rule must have a base case.

第二个子句稍微复杂一些.它大致是这样说的:要计算非空列表中元素的总和,首先切掉初始元素,然后计算得到的较短列表中元素的总和.调用该总和 Sum1. 现在通过将初始元素的值与 Sum1 的值相加来计算 Total.

The second clause is a little more complex. It says roughly this: "to compute the sum of elements in a non-empty list, first chop off the initial element, and compute the sum of elements in a shorter list that results. Call that sum Sum1. Now compute the Total by adding the value of the initial element to the value of Sum1.

第二个子句递归地将列表分解为一系列较短的列表,直到它们成为空列表.此时第一个子句介入,提供空列表的总和.

The second clause recursively decomposes the list into a series of shorter lists until they get to an empty list. At this point the first clause steps in, providing the sum of an empty list.

考虑这个例子:

list_sum([12, 34, 56], X)
    list_sum([34, 56], <unknown-1>)
        list_sum([56], <unknown-2>)
            list_sum([], 0)         ---> succeeds with Total bound to 0
        <unknown-2> becomes 0 + 56  ---> succeeds with Total bound to 56
    <unknown-1> becomes 0 + 56 + 34 ---> succeeds with Total bound to 90
X becomes 0 + 56 + 34 + 12          ---> succeeds with X bound to 102

这是有效的,因为递归链中的每个调用级别都有自己的 Sum1 变量.这些值开始时是无界的,但是一旦递归调用链触底",Sum1 就会开始获取每个先前级别计算的值.最终,调用链到达顶层,将最终结果绑定到调用者传递的变量上.

This works because each invocation level in the recursive chain gets its own variable for Sum1. These values start unbounded, but once the recursive invocation chain "bottoms out", Sum1s start getting values computed by each prior level. Eventually, the call chain reaches the top level, binding the final result to the variable passed by the caller.

这篇关于Prolog中列表中元素的总和几乎不需要解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆