需要运行此递归5次。并且每次都增加Row。
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).
换句话说,我想像往常一样(Row< Col)......
如果Col == Row,则跳过该步骤。
答案 0 :(得分:1)
我在简单的Prolog程序中通常需要命令参数。 没有规范,我可以猜测你的代码,注意我交换了M和Rest。验证第一个参数的含义......
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).
编辑:添加if Col \= Row ... else ...
条件,请注意此构造它有点不寻常,请参阅文档here。注意第一个参数无论如何都要'读'。如果必须保持不变,最好添加另一个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