質問

I have an arbitrarily entered sequence of symbols, for example progressive and must determine whether it contains an entered sequence of symbols, for example progress. My idea is to break the strings into lists of letters and compare lists.

'break'(Str,L):-'name'(Str,L1),'change'(L1,L).
'change'([],[]).
'change'([X|T],[S|T1]):-'name'(S,[X]),'change'(T,T1).

'break'(progressive,X).
X = [p, r, o, g, r, e, s, s, i|...].

But I have no idea how to break both words and to compare lists.

Examples of working program:

contain([progressive, progress]).   - Yes.
contain([progressive, progrev]).    - No.
役に立ちましたか?

解決

No need to put predicate names in quotation marks (unless you use spaces or some other non-alphabet characters, but usually you shouldn't do it).

And it's better to use ISO-standard atom_codes instead of name.

Here is a somewhat high-level implementation of what you want:

contain(X, Y) :-
    atom_codes(X, Xcodes),
    atom_codes(Y, Ycodes),
    append(_, End, Xcodes),
    append(Ycodes, _, End).

Test run (I use contain(progressive, progress). instead of contain([progressive, progress]). given in your example because it makes more sense to me. If you need to conform the example exactly, just change the head of the predicate contain(X, Y) to contain([X, Y])):

?- contain(progressive, progress).
true 

?- contain(progressive, progrev).
false.

他のヒント

I think a better solution would be to use sub_atom/5: it's ISO approved and provides much more functionality for text analysis. An example of usage

9 ?- sub_atom('swi-prolog', _,_,_, o).
true ;
true ;
false.

10 ?- sub_atom('swi-prolog', N,_,_, o).
N = 6 ;
N = 8 ;
false.
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top