I am implementing a words/2
predicate in which a list of characters can be rendered to a list of the character as a word inside a list. I use the mathematical symbol <=>
to denote that they're working in any mode. Please advise if there's a better expression.
The example:
?- words([p,r,o,l,o,g,' ',i,s,' ',g,o,o,d],Y).
Y = [[p,r,o,l,o,g],[i,s],[g,o,o,d]]
?- words(X,[[p,r,o,l,o,g],[i,s],[g,o,o,d]]).
X = [p,r,o,l,o,g,' ',i,s,' ',g,o,o,d]
What I've done is trying to use append/3
as below, trying to put the empty string in between, and concatenate them together. And recurse the List
with NewCharList
, but failed with "out of local stack".
% base case, empty list
words([],[]).
% X is a list of characters
% Y is a list with characters list as a word
words(CharList,[WordList|List]):-
append(WordList,[' '],NewWord),
append(NewWord,CharList,NewCharList),
words(NewCharList,List).
How could I improve the code? Thanks.
Edit1 Million thanks from @rajashekar. Now I understand the code
% base case
split(_, [], [[]]).
% when the the element list of Ys is empty
% add a C to the Xs, in this case, C is a empty string ' '
split(C, [C|Xs], [[]|Ys]) :-
split(C, Xs, Ys).
% put the X character from [X|Y] list to Xs
% and goes on next word list
split(C, [X|Xs], [[X|Y]|Ys]) :-
split(C, Xs, [Y|Ys]).
but in my SWI-prolog, it seems weird that:
?-split(' ', X, [[p,r,o,l,o,g],[i,s],[g,o,o,d]]).
X = [p, r, o, l, o, g, ' ', i, s|...]
Here is the trace record:
[trace] ?- split(X, [[p,r,o,l,o,g],[i,s],[g,o,o,d]]).
Call: (10) split(_5702, [[p, r, o, l, o, g], [i, s], [g, o, o, d]]) ? creep
Call: (11) split(_6210, [[r, o, l, o, g], [i, s], [g, o, o, d]]) ? creep
Call: (12) split(_6266, [[o, l, o, g], [i, s], [g, o, o, d]]) ? creep
Call: (13) split(_6322, [[l, o, g], [i, s], [g, o, o, d]]) ? creep
Call: (14) split(_6378, [[o, g], [i, s], [g, o, o, d]]) ? creep
Call: (15) split(_6434, [[g], [i, s], [g, o, o, d]]) ? creep
Call: (16) split(_6490, [[], [i, s], [g, o, o, d]]) ? creep
Call: (17) split(_6546, [[i, s], [g, o, o, d]]) ? creep
Call: (18) split(_6596, [[s], [g, o, o, d]]) ? creep
Call: (19) split(_6652, [[], [g, o, o, d]]) ? creep
Call: (20) split(_6708, [[g, o, o, d]]) ? creep
Call: (21) split(_6758, [[o, o, d]]) ? creep
Call: (22) split(_6814, [[o, d]]) ? creep
Call: (23) split(_6870, [[d]]) ? creep
Call: (24) split(_6926, [[]]) ? creep
Exit: (24) split([], [[]]) ? creep
Exit: (23) split([d], [[d]]) ? creep
Exit: (22) split([o, d], [[o, d]]) ? creep
Exit: (21) split([o, o, d], [[o, o, d]]) ? creep
Exit: (20) split([g, o, o, d], [[g, o, o, d]]) ? creep
Exit: (19) split([' ', g, o, o, d], [[], [g, o, o, d]]) ? creep
Exit: (18) split([s, ' ', g, o, o, d], [[s], [g, o, o, d]]) ? creep
Exit: (17) split([i, s, ' ', g, o, o, d], [[i, s], [g, o, o, d]]) ? creep
Exit: (16) split([' ', i, s, ' ', g, o, o, d], [[], [i, s], [g, o, o, d]]) ? creep
Exit: (15) split([g, ' ', i, s, ' ', g, o, o|...], [[g], [i, s], [g, o, o, d]]) ? creep
Exit: (14) split([o, g, ' ', i, s, ' ', g, o|...], [[o, g], [i, s], [g, o, o, d]]) ? creep
Exit: (13) split([l, o, g, ' ', i, s, ' ', g|...], [[l, o, g], [i, s], [g, o, o, d]]) ? creep
Exit: (12) split([o, l, o, g, ' ', i, s, ' '|...], [[o, l, o, g], [i, s], [g, o, o, d]]) ? creep
Exit: (11) split([r, o, l, o, g, ' ', i, s|...], [[r, o, l, o, g], [i, s], [g, o, o, d]]) ? creep
Exit: (10) split([p, r, o, l, o, g, ' ', i|...], [[p, r, o, l, o, g], [i, s], [g, o, o, d]]) ? creep
X = [p, r, o, l, o, g, ' ', i, s|...] .
EDIT2
I found out that using !
(cut) could cut down the backtracking.
% base case
split(_, [], [[]]).
% when the the element list of Ys is empty
% add a C to the Xs, in this case, C is a empty string ' '
split(C, [C|Xs], [[]|Ys]) :-
split(C, Xs, Ys),!.
% put the X character from [X|Y] list to Xs
% and goes on next word list
split(C, [X|Xs], [[X|Y]|Ys]) :-
split(C, Xs, [Y|Ys]),!.