我有一个知识库,它由一个文件"students.pl"中的学生数据库组成,如下所示:
% student(Name,Percent,List_of_Marks_in_3_subjects).
student('abc',83,[80,80,90]).
student('pqr',70,[70,60,80]).
student('xyz',76,[80,70,80]).
我想访问知识库中的每个学生谓词,并计算每个科目的平均分数或平均百分比,而不使用"findall"或断言/收回。我可能想像这样使用回溯:
find_score_all(X) :- student(Name,Percent,L),
write(Percent),nl,
fail.
find_score_all(_).
使用这种方法,我可以访问每个元素并编写它,但如果我想将每个"Percent"值作为元素添加到列表中,或者只使用"Percent1 is Total+Percent"之类的谓词来合计百分比值,然后找到其平均值,我该怎么做?请注意,我不想使用findall或retract/assert,最好在一次遍历知识库时找到平均值,因为知识库非常大。
感谢您的帮助。
%solution for sum of percents, you can replace with any other calculation sum_percent predicate.
listing(student/3, convert_to_list/2, sum_percent, sum_percent/2).
% student(Name,Percent,List_of_Marks_in_3_subjects).
student('abc',83,[80,80,90]).
student('pqr',70,[70,60,80]).
student('xyz',76,[80,70,80]).
convert_to_list(X, R):-
student(N, P, LM),
not(member(st(N, P, LM), X)),
convert_to_list([st(N, P, LM)|X], R).
convert_to_list(X, X).
sum_percent:-
convert_to_list([], X),
sum_percent(X, S),
write(S).
sum_percent([], 0).
sum_percent([st(_,E,_)|T], S):-
sum_percent(T, S2),
S is E+S2.
如果您想添加到列表中,那么您应该使用findall或更好的库(聚合)。但如果你担心效率,你可以使用类似的东西
integrate(ave, Goal, Ave) :-
State = state(0, 0, _),
repeat,
( call(Goal, V),
arg(1, State, C), U is C+1, nb_setarg(1, State, U),
arg(2, State, S), T is S+V, nb_setarg(2, State, T),
fail
; arg(1, State, C), arg(2, State, S), Ave is S/C
).
:- meta_predicate integrate(+, :, ?).
测试:
members(X) :- member(X, [1,2,3,4]).
?- integrate(ave, members, R).
R = 2.5 .
当然,您需要添加错误处理(至少在计数器C == 0
时)。