prolog中列表的常见子序列

Nik*_*ola 2 prolog

我正在尝试编写一个谓词共同(L,S),它从L列表中生成的列表L中列表的所有公共子序列.

subseq([], _).
subseq([H|S], L) :- append(_, [H|T], L), subseq(S, T).


common(L, X) :- not((
        member(A, L),
        not(subseq(X, A))
       )).
Run Code Online (Sandbox Code Playgroud)

即使输入错误,它也只是给我"真实".

例如:

common([[1,2,3,4], [2,3], [12]], X). true

编辑

我注意到它实际上正在工作,但它不是用谓词为真的术语替换X.

fal*_*lse 9

子字符串是后缀的非空前缀.

substring_of(Ys, Xs) :-
   Ys = [_|_],           %  a non-empty
   append(_, Zs, Xs),    %                       a suffix of Xs
   append(Ys, _, Zs).    %             prefix of 

common(Xss, Xs) :-       % Xs is a substring of each element of Xss
   maplist(substring_of(Xs), Xss).

?- common([[1,2,3,4], [2,3,4,5]], Xs).
Xs = [2] ;
Xs = [2, 3] ;
Xs = [2, 3, 4] ;
Xs = [3] ;
Xs = [3, 4] ;
Xs = [4] ;
false.
Run Code Online (Sandbox Code Playgroud)