在Prolog中将CYK表的一个子列表等同于另一个子列表
我目前正在开发一个Prolog程序,在得到一组产品后,它将生成一个CYK解析表。但是,我在检查两行是否相等时遇到问题。以下是我目前掌握的情况:在Prolog中将CYK表的一个子列表等同于另一个子列表,prolog,cyk,Prolog,Cyk,我目前正在开发一个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) :- to
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的初始长度设置回其原始数字
任何输入/反馈都将非常精彩,非常感谢 如果我正确理解了您的问题,您需要检查两个列表中位于相同位置的两个列表是否具有相同的元素。您可以这样做:
check([],_).
check([H|T],L):-
member(H,L),
check(T,L).
sublist_check([],[]).
sublist_check([H1|T1],[H2|T2]):-
check(H1,H2),
sublist_check(T1,T2).
?- sublist_check([["A"],["B","C"],["B","C"]],[["A"],["C","B"],["C","B"]]).
true
?- sublist_check([["A"],[],[]], [[],["A"],[]]).
false
这是damianodamiano答案(+1)的较短编码 使用library(yall),它更具吸引力:
check2(S,L) :- maplist({L}/[H]>>member(H,L), S).
sublist_check2(A,B) :- maplist(check2, A,B).
图书馆(雅尔)这不是唯一的。。。
之后
你可以
:- use_module(library(lambda)).
check3(S,L) :- maplist(\H^member(H,L),S).
sublist_check3(A,B) :- maplist(check3, A,B).
/
不会在SWI中开始评论…很抱歉,但我认为你完全错了。Prolog是关于关系编程的,试着改变你的思维方式。CYK是学习序言的好方法。。。。太可悲了……这个equal(X,Y):-sort(X,Y)。
是对equal(X,Y):-sort(X,X1),sort(Y,Y1),X1==Y1的改进。
?或者你真的需要检查变量的状态?
?- pack_install(lambda).
:- use_module(library(lambda)).
check3(S,L) :- maplist(\H^member(H,L),S).
sublist_check3(A,B) :- maplist(check3, A,B).