1

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?

Wouter Beek
  • 3,307
  • 16
  • 29
na899
  • 163
  • 1
  • 2
  • 9

2 Answers2

1

Prolog predicates that you run interactively do not carry the state. When you run

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

the interpreter shows you an assignment for L, and then forgets its value. When you run member/2 on the next line, L is back to its free variable state.

If you want the same L to carry over, you need to stay within the same request, like this:

:- atomic_list_concat(L,' ', 'This is a string'),
   member(is, L),
   writeln('membership is confirmed').

Now L assignment from atomic_list_concat is available to member/2, letting it check the membership.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Oh ! Thanks again, I am new to prolog and stackoverflow. So any tip is useful. I have another question but I believe I cannot tag specific users for answers. Can I? – na899 Dec 02 '14 at 20:05
  • 1
    I have posted another question related to prolog. http://stackoverflow.com/questions/27258188/prolog-eliminating-cycles-from-indirect-relation please take a look. – na899 Dec 02 '14 at 20:23
1

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.

Community
  • 1
  • 1
Wouter Beek
  • 3,307
  • 16
  • 29