Search code examples
prologswi-prologprolog-toplevel

search in a split a sentence in prolog


I have a list L created as:

atomic_list_concat(L,' ', 'This is a string').

L = ['This',is,a,string]

Now I want to search an atom in L using member function. I tried :

?- member(' is',L).
L = [' is'|_G268] .

?- member( is,L).
L = [is|_G268] .

What is it that I am doing wrong here?


Solution

  • Although the solution posted by dasblinkenlight is correct, it somewhat breaks the interactive nature of using the Prolog top-level. Typically, you want to base your next query on the previous solution.

    For this reason it is possible to reuse top-level bindings by writing $Var where Var is the name of a variable used in a previous query.

    In your case:

    ?- atomic_list_concat(L, ' ', 'This is a string').
    L = ['This', is, a, string].
    ?- member(' is', $L).
    false.
    ?- member('is', $L).
    true ;
    false.
    

    PS: Notice that you would not get a result when searching for ' is' since separators are removed by atomic_list_concat/3.