0
嗨,大家好,我是Prolog的新手。 我試圖在第3課中結合1000個元素而不重複。 我已經做了代碼,但我有堆棧問題。Prolog跳出全球堆棧
member(T, [T|R], R) :- !.
member(T, [_|L], R) :- member(T, L, R).
last_element([H], H) :- !.
last_element([H|[X]], X) :- !.
last_element([H|T], R) :- last_element(T, R).
macronutrienti(1).
macronutrienti(2).
macronutrienti(3).
and so on
percentuale_macronutrienti(Alimento, R) :-
macronutrienti(Alimento),
R = Alimento.
combina(NRAlimenti, RIS) :-
findall(PM, percentuale_macronutrienti(Alimento, PM), PMR),
f1(NRAlimenti, PMR, PMR, RIS), !.
f1(1, RParz, PMR, RParz) :- !.
f1(Index, RParz, PMR, RIS) :-
Index1 is Index - 1,
g2(RParz, PMR, RIS1, RIS1),
f1(Index1, RIS1, PMR, RIS).
f1(Index, RParz, PMR, RIS) :-
Index1 is Index - 1,
g1(RParz, PMR, RIS1, RIS1),
f1(Index1, RIS1, PMR, RIS).
g1([A|[TA]], A1, OldR, R) :-
q([A], L1, A1),
h1(A, L1, OldR, OldR), !.
g1([A|T], A1, Risultato, R) :-
q([A], L1, A1),
h1(A, L1, OldR, OldR),
append(OldR, TOldR,Risultato),
g1(T, A1, TOldR, R).
q(A, R, A1) :- last_element(A, LastElement), member(LastElement, A1, R).
g2([A|[TA]], A1, OldR, R) :-
q(A, L1, A1),
h2(A, L1, OldR, OldR), !.
g2([A|T], A1, Risultato, R) :-
q(A, L1, A1),
h2(A, L1, OldR, OldR),
append(OldR, TOldR, Risultato),
g2(T, A1, TOldR, R).
h1(_, [], _, _) :- !.
h1(Alimento, [Alimento1], [OldM], R) :- append([Alimento], [Alimento1], OldM).
h1(Alimento, [Alimento1|T1], [OldM|TOldM], NewM) :-
append([Alimento], [Alimento1], OldM),
h1(Alimento, T1, TOldM, NewM).
h2(_, [], _, _) :- !.
h2(Alimento, [Alimento1], [OldM], R) :- append(Alimento, [Alimento1], OldM).
h2(Alimento, [Alimento1|T1], [OldM|TOldM], NewM) :-
append(Alimento, [Alimento1], OldM),
h2(Alimento, T1, TOldM, NewM).
:- combinew(3,R).
出了什麼問題?謝謝你在前進
可能prolog卡在某種無限循環或搜索。使用trace來檢查prolog試圖找到解決方案的方式。 – 2011-06-08 21:40:06
相同的代碼工作爲400 macronutrienti(n),但如果我插入1000 macronutrienti(n),prolog走出堆棧。問題不是無限循環,但我認爲是遞歸調用的數量。 – 2011-06-09 14:08:28
在這種情況下,您可以嘗試擴展堆棧:http://www.swi-prolog.org/pldoc/doc_for?object=set_prolog_stack/2 – 2011-06-09 21:04:08