列表的prolog平均值

列表的prolog平均值,prolog,Prolog,我有代码,能够计算一个列表的平均值,但唯一的问题是它有错误时,它是空的名单。当列表为空时,我想返回false。有什么提示吗 avg( List, Avg ):- sum( List, Sum ), length( List, Length), Avg is Sum / Length. 当然,除零通常是不允许的。如果(按照惯例)0个元素的平均值可以为0,则可能需要进行校正 avg( List, Avg ):- sum( List, Sum ), le

我有代码,能够计算一个列表的平均值,但唯一的问题是它有错误时,它是空的名单。当列表为空时,我想返回false。有什么提示吗

avg( List, Avg ):- 
    sum( List, Sum ),
    length( List, Length), 
    Avg is Sum / Length.

当然,除零通常是不允许的。如果(按照惯例)0个元素的平均值可以为0,则可能需要进行校正

avg( List, Avg ):- 
    sum( List, Sum ),
    length( List, Length), 
    (  Length > 0
    -> Avg is Sum / Length
    ;  Avg is 0
    ).
编辑我在SWI Prolog中测试,将sum/2替换为sumlist/2

avg( List, Avg ):-
    sumlist( List, Sum ),
    length( List, Length),
    (  Length > 0
    -> Avg is Sum / Length
    ;  Avg is 0
    ).
测试:

编辑很抱歉,我忽略了空输入列表中需要
false
。然后,如果
Length>0
测试肯定是合适的。谓词将在这方面失败

使用库(聚合)的替代方法:


当然,除零通常是不允许的。如果(按照惯例)0个元素的平均值可以为0,则可能需要进行校正

avg( List, Avg ):- 
    sum( List, Sum ),
    length( List, Length), 
    (  Length > 0
    -> Avg is Sum / Length
    ;  Avg is 0
    ).
编辑我在SWI Prolog中测试,将sum/2替换为sumlist/2

avg( List, Avg ):-
    sumlist( List, Sum ),
    length( List, Length),
    (  Length > 0
    -> Avg is Sum / Length
    ;  Avg is 0
    ).
测试:

编辑很抱歉,我忽略了空输入列表中需要
false
。然后,如果
Length>0
测试肯定是合适的。谓词将在这方面失败

使用库(聚合)的替代方法:


只需添加列表不为空的条件,例如:

average( List, Average ):- 
    sum( List, Sum ),
    length( List, Length ),
    Length > 0, 
    Average is Sum / Length.

这将导致谓词失败,这是适当的,因为平均值未定义。

只需添加列表不为空的条件,例如:

average( List, Average ):- 
    sum( List, Sum ),
    length( List, Length ),
    Length > 0, 
    Average is Sum / Length.
这将导致谓词失败,这是适当的,因为平均值未定义。

avg(列表,avg/1):-

你应该把平均数除以1。 例如平均值(列表,平均值/1)。我只是复制了上面评论中给出的内容,并添加了avg/2。

avg(列表,avg/1):-

你应该把平均数除以1。
例如平均值(列表,平均值/1)。我只是复制了上面评论中给出的结果,并添加了avg/2。

使用累加器,这意味着,一个计数器来实现算术平均可能有助于理解Prolog的工作原理

mean([],0). % an empty list would have mean 0

mean(Xs,Mean):- % define the predicate with a list, and the mean output
  % pass the original list, mean output, and adds two counters
  % the counters should start at 0 for accumulation
  mean1(Xs,0,0,Mean). 

% base case, after recursion, the list would be empty
% the counter would accumulate the number of elements and
% the sum of the elements
mean1([],Count,Sum,Mean):- 
  Mean is Sum / Count.

% basic recursion to track the EleCount, plus 1 each time
% the Sum would add the X in the Xs each time, until Xs is empty
mean1([X|Xs],EleCount,Sum,Mean):-
  NewCount is EleCount + 1,
  NewSum is Sum + X,
  mean1(Xs,NewCount,NewSum,Mean).

使用累加器,也就是说,一个计数器来实现算术平均可能有助于理解Prolog是如何工作的

mean([],0). % an empty list would have mean 0

mean(Xs,Mean):- % define the predicate with a list, and the mean output
  % pass the original list, mean output, and adds two counters
  % the counters should start at 0 for accumulation
  mean1(Xs,0,0,Mean). 

% base case, after recursion, the list would be empty
% the counter would accumulate the number of elements and
% the sum of the elements
mean1([],Count,Sum,Mean):- 
  Mean is Sum / Count.

% basic recursion to track the EleCount, plus 1 each time
% the Sum would add the X in the Xs each time, until Xs is empty
mean1([X|Xs],EleCount,Sum,Mean):-
  NewCount is EleCount + 1,
  NewSum is Sum + X,
  mean1(Xs,NewCount,NewSum,Mean).

是否可以是sum/2?是否未定义?或者Prolog没有实现->/2?。查看我的编辑…哦,对不起,最近做了很多Lisp并阅读了
作为注释。sum/2是否未定义?或者Prolog没有实现->/2?。查看我的编辑…哦,对不起,最近做了很多Lisp并阅读了
作为注释。