How to manipulate strings in prolog?

2019-05-31 00:40发布

问题:

I have this fact in my base fact("name","surname","123"). if i simply write this question: fact(X,_,_). For X I get some unidentified output. How can I retrieve any of this values, or how to get this output? ?-fact(X,_,_). output: name.

Thanks ahead.

回答1:

In SWI-Prolog you can use string_to_atom/2:

?- assert(fact("name", "surname", "123")).
true.

?- fact(Tmp, _, _), string_to_atom(Tmp, X).
Tmp = [110, 97, 109, 101],
X = name.


回答2:

Strings in Prolog are enclosed within single quotes. When you use double quotes it means that you want the list of character codes.

?- is_list('abc').
false.

?- is_list("abc").
true.

?- write("abc").
[97,98,99]
true.

?- write('abc').
abc
true.


回答3:

try this,

| ?- assert(fact("name", "surname", "123")).
yes
| ?- fact(X,_,_).
X = [110,97,109,101];
| ?- fact(_X,_,_),name(Y,_X).
Y = name;


标签: string prolog