0

I've spend whole day on the internet and I wasn't able to find any built-in predicate in Win-Prolog that can single out every word in a String.

Example:

| ?- read(X).
|: 'this is a string'.
X = 'this is a string'.

is there any predicate I can use that will help me, single out every word of in the string? like

A = this
B = is
C = a
D = string

or a list

A = [This, is, a, string]

is it possible?

Soon
  • 1
  • 4

1 Answers1

0

In Win-Prolog you might use:

split_string(String, List):-
  string_chars(String, LChar),
  split_string(List, LWord-LWord, LChar, []).

split_string([Word|TailWords], LWord-[])--> 
     " ", 
     {string_chars(Word, LWord)}, 
     split_string(TailWords, NLWord-NLWord).
split_string(Words, Head-[Char|LWord]) --> 
     [Char], 
     {[Char] \= " "}, 
     split_string(Words, Head-LWord).
split_string([Word], LWord-[])--> 
     [], 
     { string_chars(Word, LWord)}.

In SWI-Prolog you can use atomic_list_concat(List, ' ', 'this is a string').

gusbro
  • 22,357
  • 35
  • 46