Gamsh
Gamsh

Reputation: 545

Split using DCG rules

I need to split dichloropentane using the rules in swi-prolog this what i try to do

stem--> [meth]|[eth]|[prop]|[but]|[pent].

suf --> [ane]|[ene]|[yne].

pre --> [di]|[tri]|[tetre].

hal--> [floro]|[chloro]|[bromo].

?- split_list(['2','3', dichloropentane],['2','3', di,chloro,pent,ane]).

In English by using Prolog I need to split dichloropentane into di,chloro,pent,ane as shown above.

I'm a real newbie for Prolog and please any Prolog pros help me out with the code.

Upvotes: 3

Views: 166

Answers (1)

CapelliC
CapelliC

Reputation: 60024

try

any(S, K) --> {member(S,K)}, S.

stem(S) --> any(S, ["meth","eth","prop","but","pent"]).
suf(S) --> any(S, ["ane","ene","yne"]).
pre(S) --> any(S, ["di","tri","tetre"]).
hal(S) --> any(S, ["floro","chloro","bromo"]).

split_list(S,[A,B,C,D]) :- phrase((pre(A),hal(B),stem(C),suf(D)),S).

and you should get

?- split_list(`dichloropentane`,P).
P = ["di", "chloro", "pent", "ane"] 

note the kind of quotes: since SWI-Prolog introduced in ver.7 the type string, coding for DCG need more care...

edit

?- atom_codes(dichloropentane,Cs), split_list(Cs,L).
Cs = [100, 105, 99, 104, 108, 111, 114, 111, 112|...],
L = ["di", "chloro", "pent", "ane"]

Upvotes: 2

Related Questions