获取创建特定单词序言的所有元素的子列表

时间:2017-08-28 00:59:02

标签: prolog

我想得到一个组合特定单词的所有元素的子列表 致电

assemble([hello,'',world,hi,bye,good,well], 'hello world', A).

程序应该打印

A=[hello,'',world]

用于通话

assemble([abc,123,ab,c,123],'abc123', A).

程序应该打印

A=[abc,123];
A=[ab,c,123];

感谢您的帮助。

matchwords(W1, W2, Results) :-
    setof(R, matchw(W1, W2, R), RSet), % Collect all the matching substrings
                                       % and their lengths
    reverse(RSet, Set),                % Order by longest first
    highest(Set, Results).             % keep only the highest ones

matchw(W1, W2, N-Result) :-
    atom_chars(W1, A1),
    atom_chars(W2, A2),
    matchl(A1, A2, R),
    length(R, N),
    atom_chars(Result, R).

matchl([H|T1], [H|T2], [H|T]) :-
    matchl(T1, T2, T).
matchl([H1|T1], [H2|T2], R) :-
    H1 \= H2,
    ( matchl(T1, [H2|T2], R) ; matchl([H1|T1], T2, R) ).
matchl([], _, []).
matchl([_|_], [], []).

highest([_-W], [W]).
highest([N1-W1,N2-_|_], [W1]) :-
    N1 > N2.
highest([N1-W1,N2-W2|T], [W1|WT]) :-
    N1 = N2,
    highest([N2-W2|T], WT).

0 个答案:

没有答案