在递归/回溯中累积 [英] Accumulating while in recursion/backtracking

查看:43
本文介绍了在递归/回溯中累积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这个初学者的问题,我不知道如何解决这个问题.这是我的代码:

I run into this beginners problem, and i don't know how to solve this. Here is my code:

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)

我需要从一个部门获得所有工资的总和,如下所示:

I need to get sum of all salaries form one of department, like this:

?- 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 解决这个问题.

now I need to sum T+T for total costs, but I don't know how to do that. I would like to solve this without using findall/setof/bagof.

我尝试使用 findall:

I tried with 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)时.我明白了:

It works fine with department_costs( d1, T ), and department_costs( d2, T ), but when I enter department_costs( D, T ). i get this:

 department_costs( D, T ).
 O=_h159
 T=11500

它应该是这样的:

 ?- department_costs( D, T ) .
 D = d1
 T = 4500;
 D = d2
 T = 7000;

有人能告诉我现在的问题是什么吗?

can someone tell what is the problem now?

推荐答案

想要在没有 findall/3 的情况下解决这个问题只会导致重新编码 findall/3 的糟糕尝试代码>.如果您真的想跳过 findall/3,请以不同的方式表示您的数据,例如:

Wanting to solve this without findall/3 will just result in a poor attempt at recoding said findall/3. If you really wanna skip findall/3, represent your data in a different way, for example:

workers([w1-d1-2000, w2-d1-2500, w2-d2-1000, w3-d2-2000, w4-d2-4000]).
departments([d1-w2, d2-w4]).

在这种格式中,您将能够使用递归和列表处理技术来获得良好的结果.在前一个中,您将不得不使用数据库操作和/或全局变量.不是真正的 Prolog-ish.

In this format you will be able to use recursion and list processing techniques to achieve a good result. In the previous one you will have to go with either database manipulation and/or global variables. Not really Prolog-ish.

对于您的编辑,问题在于您使用 findall/3,而 findall/3 将为您提供您感兴趣的一个变量的所有结果在,但不会精确导致这些结果的绑定.

For your edit, the problem is that you use findall/3, and that findall/3 will give you all the results for the one variable you're interested in, but will not precise the bindings that led to those results.

试试bagof/3:

bagof(S, W^worker( W, D, S ), R ).

并查看此 手册页(即使是 SWI,无论如何这些都是 ISO Prolog 谓词)了解更多信息.

And see this man page (even if it's SWI, those are ISO Prolog predicates anyway) for more information.

这篇关于在递归/回溯中累积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆