Question

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.
Était-ce utile?

La solution

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.

Autres conseils

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.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top