我遇到了这个初学者问题,我不知道如何解决这个问题。这是我的代码:
worker( w1, d1, 2000 ) .
worker( w2, d1, 2500 ) .
worker( w2, d2, 1000 ) .
worker( w3, d2, 2000 ) .
worker( w4, d2, 4000 ) .
% worker( W, D, S ) means that worker W works in department D and has salary S
department( d1, w2 ) .
department( d2, w4 ) .
% department( D, B ) means that worker B is director of department D(this is not important in this case)
我需要从一个部门获得所有工资的总和,如下所示:
?- department_costs( d1 , T ) .
T = 4500;
no
?- department_costs( D, T ) .
D = d1
T = 4500;
D = d2
T = 7000;
no
?- department_costs( d3 , T ) .
no
我试过这个:
department_costs( D, T ):- worker( _X, D, T1 ), T is T1.
我明白了:
?- department_costs( o1, T ).
T=2000;
T=2500;
no
现在我需要对总成本求和 T+T,但我不知道该怎么做。我想在不使用 findall/setof/bagof 的情况下解决这个问题。
编辑:
我尝试使用 findall:
sumL([], 0).
sumL([G|R], S):-
sumL(R, S1),
S is S1 + G.
department_costs( D, T ):-
findall(P, worker( _X, D, P ), R ),
sumL(R, S),
T=S.
它适用于部门成本(d1,T)和部门成本(d2,T),但是当我输入部门成本(D,T)时。我明白了:
department_costs( D, T ).
O=_h159
T=11500
它应该是这样的:
?- department_costs( D, T ) .
D = d1
T = 4500;
D = d2
T = 7000;
有人可以告诉现在是什么问题吗?