prolog 中的 My_append 函数不起作用?

问题描述 投票:0回答:1

我有一个代码最后返回一个列表,我必须在最后添加一个“o”,但是 my_append 函数在测试它时有效,但在我运行我的代码时却不起作用

% Color definition
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 applies
% 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).

my_append(L, [], [L]).
my_append(L1,[L |L2], [L|L3]) :- my_append(L1, L2, L3).
prolog
1个回答
0
投票

我最终得到了这个代码:

% Color definition
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).
© www.soinside.com 2019 - 2024. All rights reserved.