Reputation: 545
I want split string into sub string using dcg list.
here my code
any(S, K) --> {member(S,K)}, S.
pre(S) --> any(S, ["di","tri","tetre"]).
split([]) --> "".
split([X|Xs]) --> pre(X), split(Xs).
?- phrase(split(Ls), "tridi").
Ls = [[t, r, i], [d, i]]
Here some error.it is split as letters,expected answer is?- phrase(split(Ls), "tridi").
Ls = [tri,di].
Any Idea for this problem?
Upvotes: 1
Views: 287
Reputation: 60004
for SWI-Prolog v.7, you need to change your code like
any(A,K) --> {member(S,K)}, S, {atom_codes(A, S)}.
pre(S) --> any(S, [`di`,`tri`,`tetre`]).
split([]) --> "".
split([X|Xs]) --> pre(X), split(Xs).
and then
?- phrase(split(Ls), `tridi`).
Ls = [tri, di]
Note I added a conversion to any//1. The rationale of string representation is documented here.
With your original code and query:
?- set_prolog_flag(double_quotes, codes).
true.
?- phrase(split(Ls), "tridi").
Ls = [tri, di]
Upvotes: 1