在Prolog中将CYK表的子列表等同于另一个子列表 [英] Equating a Sublist to Another Sublist for CYK table in Prolog

查看:78
本文介绍了在Prolog中将CYK表的子列表等同于另一个子列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究Prolog程序,该程序将在得到一系列结果后生成CYK解析表.但是,我在检查两行以查看它们是否相等时遇到麻烦.这是我到目前为止的内容:

I'm currently working on a Prolog program that will generate a CYK parse table after being given a set of productions. However, I am having troubles checking two rows to see if they are equivalent. Here's what I have so far:

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的初始长度设置回其原始长度的错误数字.

I think my issue is that I need to find a way to create a variable equivalent to the max length of the list and decrement it every time, but I run into the error of setting the initial length of sublist_check back to its original number.

非常感谢任何输入/反馈!

Any input/feedback would be brilliant, thanks so much!

推荐答案

如果我正确理解了您的问题,则要检查两个列表中位于相同位置的两个列表是否具有相同的元素.您可以通过以下方式做到这一点:

If i've understood properly your problem, you want to check if the two lists in the same position in the two list of lists, have the same elements. You can do it in this way:

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

这篇关于在Prolog中将CYK表的子列表等同于另一个子列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆