计算递归函数求和。

huangapple go评论56阅读模式
英文:

Prolog: Calculate summation by recursive function

问题

我必须在Prolog中编写一个递归函数,给定两个整数X>0和N>=2,计算以下和:

您编写的代码如下:

sum(1,2,9).
sum(X,N,Y):-
   X>1,
   N>2,
   N1 is N-1,
   sum(X,N1,Y1),
   Y is Y1 + (((4*X+N)^2)/2*N).

?- sum(3,4,Y).
false, unexpected. % why?

我弄不清楚我错在哪里。

英文:

I have to write a recursive function in prolog which, given two integers X>0 and N>=2, calculates the following sum:

计算递归函数求和。

The code I wrote is as follows:

sum(1,2,9).
sum(X,N,Y):-
   X>1,
   N>2,
   N1 is N-1,
   sum(X,N1,Y1),
    Y is Y1 + (((4*X+N)^2)/2*N).

 ?- sum(3,4,Y).
    false, unexpected.   % why?

I can't figure out where I'm wrong.

答案1

得分: 2

为了查明程序失败的原因,通常有助于将程序进行泛化。如果泛化本身失败了,那么错误必须在剩下的部分中。我尝试了:

:- op(950, fy, *).
*(_).

sum(1,2,9).
sum(X,N,Y):-
   X > 1,
   N > 2,
   N1 is N-1,
   sum(X,N1,Y1),
   * <s>Y is Y1 + (((4*X+N)^2)/2*N)</s>.

?- sum(3,4, Y).
   false, unexpected. % 仍然失败

因此,求和项的公式与此错误无关。
请注意第一个参数及其变量 X。它总是相同的。因此,事实中的值 3 和值 1 确保永远不会有解。

英文:

To see why a program fails, it often helps to generalize the program. And if that generalization itself fails, then an error must be in the remaining part. I tried:

<pre>
:- op(950, fy, *).
*(_).

sum(1,2,9).
sum(X,N,Y):-
X>1,
N>2,
N1 is N-1,
sum(X,N1,Y1),

  • <s>Y is Y1 + (((4X+N)^2)/2N)</s>.

?- sum(3,4, Y).
false, unexpected. % still fails
</pre>

So the formula for the summand is unrelated to this error.
Note the first argument and its variable X. It is always the same. Thus the value 3 and the value 1in the fact ensure that there will never be a solution.

答案2

得分: 1

I would write the function being summed as

f(I,X,V) :- V is (4 * X + I)**2 / (2*I) .

making it trivial to test:

?- f(3,4,V)
V = 49

Once you have that, write your recursive predicate, something like this:

% this exists solely to enforce the constraints
% - X must be a number
% - N must be an integer greater than or equal to 2
%
% it then invokes a helper predicate, seeing that with an initial value for
% the accumulator.
sum(N,X,S) :-
  number(X),
  integer(N),
  N >= 2,
  sum(N,X,0,S)
  .

The helper predicate, sum/4 looks like this:

sum(I,X,T,S) :-
    I >= 2,
    f(I,X,V),
    T1 is T+V,
    I1 is I-1,
    sum(I1,X,T1,S) .
sum(1,_,S,S) .

Link to the code

英文:

I would write the function being summed as

f(I,X,V) :- V is (4 * X + I)**2 / (2*I) .

making it trivial to test:

?- f(3,4,V)
V = 49

Once you have that, write your recursive predicate, something like this:

% this exists solely to enforce the constraints
% - X must be a number
% - N must be an integer greater than or equal to 2
%
% it then invokes a helper predicate, seeing that with an initial value for
% the accumulator.
sum(N,X,S) :-
  number(X),
  integer(N),
  N &gt;= 2,
  sum(N,X,0,S)
  .

The helper predicate, sum/4 looks like this:

sum(I,X,T,S) :-
    I &gt;= 2,
    f(I,X,V),
    T1 is T+V,
    I1 is I-1,
    sum(I1,X,T1,S) .
sum(1,_,S,S) .

https://swish.swi-prolog.org/p/DfKjFjMd.pl



</details>



# 答案3
**得分**: 0

一种方法是使用库,例如 SWI-Prolog 的 library(aggregate)。一种方法是这样做:

```prolog
my_function(X, I, R) :-
    R is (4*X + I)^2 / (2*I).

summation(Lower, Upper, Fun, Sum) :-
    aggregate_all(sum(R),
        (   between(Lower, Upper, I),
            call(Fun, I, R)
        ),
        Sum).

使用这个:

?- summation(2, 4, my_function(3), S).
S = 118.5.
英文:

One way to do it is to use a library, for example SWI-Prolog's library(aggregate). One way to do it:

my_function(X, I, R) :-
    R is (4*X + I)^2 / (2*I).

summation(Lower, Upper, Fun, Sum) :-
    aggregate_all(sum(R),
        (   between(Lower, Upper, I),
            call(Fun, I, R)
        ),
        Sum).

With this:

?- summation(2, 4, my_function(3), S).
S = 118.5.

huangapple
  • 本文由 发表于 2023年6月29日 15:36:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76578939.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定