Привет, ребята, я новичок в Прологе.
Я пытаюсь объединить 1000 элементов в классе 3 без повторений.
Я сделал код, но у меня проблема со стеком.
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).
Что не так? Заранее спасибо