Prolog 脱离全局堆栈
大家好,我是 Prolog 新手。 我试图在不重复的情况下组合第 3 类中的 1000 个元素。 我已经完成了代码,但我遇到了堆栈外问题。
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).
怎么了?先感谢您
Hi guys i'm new on Prolog.
I'm trying to combine 1000 elements in class 3 without repetition.
I have done the code, but i have out of stack problem.
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).
What is wrong? Thank you in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个更短的代码(我认为)与您的代码相同:(
我稍微更改了您的
member/3
谓词)但是,这仍然无法解决您的堆栈溢出问题。基本上,如果我正确理解你的问题,你想从 1000 个元素的集合中提取长度为 3 的所有排序子集。您正在查看的集合数量是 1000!/(1000-3)!,即 997.002.000 个长度为 3 的列表。这相当多,所以如果您需要一个巨大的堆栈。
如果您不需要完整列表才能继续,请更改您的工作流程以一次生成一个此类项目,然后立即处理它,并继续下一个。
Here's a much shorter code that (I think) does the same as yours:
( I slightly changed your
member/3
predicate)However, this still does not resolve your stack overflow problem. Basically, if I understood your question correctly, you want to extract all sorted subsets of length 3 from the set of 1000 elements. The number of sets you are looking at is 1000!/(1000-3)!, i.e., 997.002.000 lists of length 3. That's quite a lot, so if you'll need a huge stack.
If you don't need the complete list to continue, change your workflow to generate one such item at a time, then process it immediately, and continue with the next.