2

私は現在、一連のプロダクションが与えられた後に CYK 解析テーブルを生成する Prolog プログラムに取り組んでいます。ただし、2 つの行をチェックしてそれらが等しいかどうかを確認するのに問題があります。これが私がこれまでに持っているものです:

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 の初期長を元の数に戻すというエラーが発生します。

どんな入力/フィードバックでも素晴らしいでしょう、どうもありがとう!

4

2 に答える 2

1

これは damianodamiano の回答 (+1) の短いコーディングです。

check1(S,L) :- maplist(member_(L), S).
sublist_check1(A,B) :- maplist(check1, A,B).

member_(L,H) :- member(H,L).

library(yall) を使用すると、より魅力的です。

check2(S,L) :- maplist({L}/[H]>>member(H,L), S).
sublist_check2(A,B) :- maplist(check2, A,B).

library(yall) 一人じゃない…あと

?- pack_install(lambda).

あなたはできる

:- use_module(library(lambda)).
check3(S,L) :- maplist(\H^member(H,L),S).
sublist_check3(A,B) :- maplist(check3, A,B).
于 2018-03-03T13:57:02.920 に答える