我目前正在开发一个 Prolog 程序,该程序将在获得一组产品后生成一个 CYK 解析表。但是,我在检查两行以查看它们是否相等时遇到了麻烦。这是我到目前为止所拥有的:
answer(X,X).
%Checks to see if it is equivalent
equal(X,Y) :- sort(X,X1), sort(Y,Y1), X1 == Y1.
%find the length of the lists
total_length([],0).
total_length([_|Xs],L) :- total_length(Xs,M), L is M+1.
%storing length of lists and possible use of a decrement here to decrement the length...but don't understand how
storing(M,N) :- total_length(L,L_length), total_length(N,N_length), L_length is N_length, answer(L_length,N_length).
%Check for row equivalence...again, trying to find a way to decrement for the recursion, but unsure how to do it
sublist_check(Ra,Rb) :- storing(Ra,Rb), nth0(X,Ra,R1), nth0(Y,Rb,R2), equal(R1,R2), sublist_check(Ra,Rb).
假设输入是:
sublist_check([["A"],[],[]], [[],["A"],[]]). -->
false.
sublist_check([["A"],["B","C"],["B","C"]],[["A"],["C","B"],["C","B"]]). -->
true.
我认为我的问题是我需要找到一种方法来创建一个等效于列表最大长度的变量并每次递减它,但是我遇到了将 sublist_check 的初始长度设置回其原始数字的错误。
任何输入/反馈都会很棒,非常感谢!