我正在尝试创建一个函数,该函数迭代 N 次我在列表上拥有的另一个函数,但我不知道为什么对于大于 0 的数字,答案是否定的。 函数单元对列表中的每个元素应用一组规则,将其转换为“x”或“o” 但是 qhen 使用函数 evol 进行 N 次迭代是行不通的。
color(o).
color(x).
% Rule definition
rule(o,o,o,_,o). % Null rule
rule(x,o,o,r(A,_,_,_,_,_,_),A) :- color(A).
rule(o,x,o,r(_,B,_,_,_,_,_),B) :- color(B).
rule(o,o,x,r(_,_,C,_,_,_,_),C) :- color(C).
rule(x,o,x,r(_,_,_,D,_,_,_),D) :- color(D).
rule(x,x,o,r(_,_,_,_,E,_,_),E) :- color(E).
rule(o,x,x,r(_,_,_,_,_,F,_),F) :- color(F).
rule(x,x,x,r(_,_,_,_,_,_,G),G) :- color(G).
cells(State, Rules, Cells) :-
triplets(State,_, Rules, Sols),
my_append(Sols,[o],Result),
Cells = Result.
% Creates a list of lists of triplets form a list given aplies
% the rules to the triplets and saves the result in the list Sols
triplets(State, _,Rules, [o|Sols]) :-
triplets_helper([o|State], Triplets,Rules, Sols).
triplets_helper([X1,X2], [[X1,X2,o]],Rules, [Sol]) :-
apply_rule(X1,X2,o,Rules,Sol),
!. % insert null at the end
triplets_helper([X1,X2,X3|Xs], [[X1,X2,X3]|Ys],Rules, [Sol|Sols]) :-
apply_rule(X1,X2,X3,Rules,Sol),
triplets_helper([X2,X3|Xs], Ys,Rules, Sols).
% Applies the rules to each triplet
apply_rule(X,Y,Z,Rules,Re) :-
rule(X,Y,Z,Rules,Re).
% Unites lists
my_append([], A, A).
my_append([H|T], A2, [H|A3]) :- my_append(T, A2, A3).
% Function evol
evol(0, r(_,_,_,_,_,_,_), Cells):-
Cells = [o,x,o].
evol(N, Rules, Cells) :-
evol_auxiliar(N, [o,x,o], Rules, Cells),
evol(N, Rules, _Cells1).
%Recursive evol
evol_auxiliar(0, Cells, _, Cells).
evol_auxiliar(N, Input, Rules, Result):-
cells(Input, Rules, Cells),
N = N - 1,
evol_auxiliar(N, Cells, Rules, Result).