Note that there are some explanatory texts on larger screens.

plurals
  1. POAccumulating while in recursion/backtracking
    primarykey
    data
    text
    <p>I run into this beginners problem, and i don't know how to solve this. Here is my code:</p> <pre><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) </code></pre> <p>I need to get sum of all salaries form one of department, like this:</p> <pre><code>?- 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 </code></pre> <p>I tried this:</p> <pre><code>department_costs( D, T ):- worker( _X, D, T1 ), T is T1. </code></pre> <p>And I get this:</p> <pre><code>?- department_costs( o1, T ). T=2000; T=2500; no </code></pre> <p>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 <strong>without</strong> using findall/setof/bagof.</p> <p><strong>EDIT:</strong></p> <p>I tried with findall:</p> <pre><code>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. </code></pre> <p>It works fine with department_costs( d1, T ), and department_costs( d2, T ), but when I enter department_costs( D, T ). i get this:</p> <pre><code> department_costs( D, T ). O=_h159 T=11500 </code></pre> <p>and it should be like this:</p> <pre><code> ?- department_costs( D, T ) . D = d1 T = 4500; D = d2 T = 7000; </code></pre> <p>can someone tell what is the problem now?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload