Prolog家庭结构工资总和

时间:2014-12-22 17:12:29

标签: list prolog sum structure addition

我希望获得一系列家庭子女的工资。我在Turbo Prolog工作。据我所知,howManyChildrenSalary在Visual Prolog中效果很好。

family (fm("Ivan","Ivanov", dat(1,"may",1980),wor("ingener",0)),
fm("Anna","Ivanov", dat(15, "january",1945),wor("doctor",1800)),
[fm("Oleg","Ivanov", dat(22, "march", 1970), wor("student",720)),
fm("Antin","Ivanov", dat(22, "march", 1970), wor("student",820)),
fm("Inna", "Ivanov", dat(7, "july", 1975), wor("pupil",100))]).    

howManyChildrenSalary([],0).
howManyChildrenSalary([fm(_,_,_,wor(_,Salary))|Children],N):-howManyChildren(Children,N1),N=Salary+N1.    

profit (Surname,Average) :-
family(X,Y,Children),
X = fm(_,Surname,dat(_,_,Year),wor(_,0)),
Y = fm(_,Surname,_,wor(_,SalaryMom)),
Year > 2014-40,
howManyChildrenSalary(Children,ChildrenSalary),
howManyChildren(Children,N),
Average = (SalaryMom+ChildrenSalary)/(N+2).

所以在我的情况下,如果ChildrenSalary = 720 + 820 + 100 = 1640,我得到ChildrenSalary = 720 + 1 + 1 = 722。 为什么我会得到这个以及如何解决它?

提前感谢!

1 个答案:

答案 0 :(得分:0)

错字:

howManyChildrenSalary([fm(_,_,_,wor(_,Salary))|Children],N):-
   howManyChildrenSalary(Children,N1),N=Salary+N1. 

假设

howManyChildren(Children,N) :- length(Children,N).