I want to split words from string and put into a list in Prolog.
num --> [one] | [two] | [three] | [four] | [five].
?- split("onethreetwofive", Ls).
Ls = [one,three,two,five]. % expected answer
Here, I want to split the string with matching list from num
and put the words in list. I am using SWI-Prolog. Any ideas? Thanks!
Lets try this code.
:-set_prolog_flag(double_quotes, codes).
any(A,K) --> {member(S,K)}, S, {atom_codes(A, S)}.
num(S) --> any(S, ["one","two","three","four","five"]).
nums([]) --> "".
nums([X|Xs]) --> num(X), nums(Xs).
split(Str,Ls):-phrase(nums(Ls),Str).
Ok Now Let's run the query.
?- split("onethreetwofive", Ls).
Ls = [one, three, two, five] ;