how to split a sentence in swi-prolog

JPro picture JPro · Oct 20, 2010 · Viewed 10.2k times · Source

I am trying my hands on SWI-Prolog in win xp. I am trying to understand how to split a sentence in Prolog into separate atoms.

Ex : Say I have a sentence like this :

"this is a string"
Is there any way to get individual words to get stored in a variable?

like :

X = this
Y = is
....
and so forth.

Can anyone please explain how this works?

Thanks.

Answer

sumx picture sumx · Feb 1, 2012

I would use atomic_list_concat/3. See

http://www.swi-prolog.org/pldoc/man?predicate=atomic_list_concat%2F3

Normally it is meant to insert a separator but because of Prolog's bidirectionality of unification, it can also be used to split a string given the separator:

atomic_list_concat(L,' ', 'This is a string').
L = ['This',is,a,string]

Of course once the split is done you can play with the elements of the list L.