Detect all k-length words of string in prolog

prolog, recursion, string

Solution

You could write:

final_kthWords(L,K,Outlist):-
         kthWords(L,K,L1),
         reverse(L1,[_|T]),
         reverse(T,Outlist).

kthWords([],_,[]):-!.
kthWords(L,K,L1):-
     find_word(L,Word,L2),
     length(Word,N),
     (N=:=K-> append(Word,[' '|T],L1),kthWords(L2,K,T);
     kthWords(L2,K,L1)).

find_word([],[],[]).
find_word([H|T],[H|T1],L):-dif(H,' '),find_word(T,T1,L).
find_word([H|T],[],T):- H = ' '.

Where `kthWords/3` calls `find_word/2` which finds the words and finally `kthWords` returns the output list but it adds an `' '` in the end. The only thing that `final_kthWords(L,K,Outlist)/3` does is removing the extra `' '` in the end of the list and returns the right list:

?- final_kthWords([w,o,r,d,1,' ',w,r,d,' ',w,o,r,d,2], 5, X).
X = [w, o, r, d, 1, ' ', w, o, r, d, 2] ;
false.

Problem

Words are any symbol characters that are separated by white-spaces or by start/end points of string. For ex. `[w,o,r,d,1,' ',w,o,r,d,2]`. I need to find all k-length words of given string and append them into the result string (separated with white-spaces). This is what I'm expecting for example in the case of k = 5: ``` ?- kthWords([w,o,r,d,1,'',w,r,d,'',w,o,r,d,2], 5, X). X = [w,o,r,d,1,'',w,o,r,d,2]. ```

Original source