我按照通常在簡單的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
我該怎麼辦,如果我想如果COL =行跳過這一步? – Waqas 2012-04-08 19:14:28
我不增加行。我需要改變行是Num + 1.如果我改變它,它會給我堆棧溢出 – Waqas 2012-04-08 21:35:16