Waqas
Waqas

Reputation: 639

Add one every time. Beginner

Need to run this recursion 5 times. and increment Row every time.

calculateSum([],List,Row,5,L) :- nl.
calculateSum([M|Rest],List,Row,Col,[Y|Tail]):-
    calcHeu(Rest,L),sum(L,S),index(List, Row, Col, V),Y is V + S,inc(Row),
    calculateSum(M,List,Row,Z,Tail).

In other word i want to run like while(Row < Col) ...

And also if Col == Row then skip the step.

Upvotes: 1

Views: 113

Answers (1)

CapelliC
CapelliC

Reputation: 60014

I ordered arguments as usually required in simple Prolog programs. Without specification, I can just guess about your code, note I swapped M and Rest. Verify the meaning of the first argument...

calculateSum(_, _List, _Row, 5, []).
calculateSum([M|Rest], List, Row, Col, Result):-
    Col < 5,
    (  Col \= Row
    -> calcHeu(M, L),
       sum(L, S),
       index(List, Row, Col, V),
       Y is V + S,
       Result = [Y|Tail]
    ;  Result = Tail
    ),
    Row1 is Row + 1,
    Col1 is Col + 1,
    calculateSum(Rest, List, Row1, Col1, Tail).

edit: added if Col \= Row ... else ... condition, beware this construct it's a bit unusual, see for instance here the documentation. Note the first argument get 'read' anyway. If that must pass unchanged, it's better to add another calculateSum/5:

calculateSum(_, _List, _Row, 5, []).
calculateSum(Input, List, Num, Num, Result):-
    Row is Num + 1,
    Col is Num,
    calculateSum(Input, List, Row, Col, Result).
calculateSum([M|Rest], List, Row, Col, Result):-
    Col < 5,
    ....

HTH

Upvotes: 1

Related Questions