Prolog returns a list instead of several possible strings
answer("Yes").
answer("No").
开发者_StackOverflow社区answer("Variable = value").
receive(A) :- answer(A).
2 ?- answer(A).
A = [89, 101, 115]
Yes
I want A = "Yes"
etc. What am I doing wrong?
You are getting the list representation of the strings Yes, No and Variable = value.
If you want to instantiate A with the terms Yes, No and Variable = value you should enclose them between single quotes instead of double quotes:
answer('Yes').
answer('No').
answer('Variable = value').
and if you want to return the terms with the double quotes included, you should include them but also enclose each term with single quotes:
answer('"Yes"').
answer('"No"').
answer('"Variable = value"').
You are doing nothing wrong. [89, 101, 115]
is the same as "Yes"
:
2 ?- [89, 101, 115] = "Yes".
true.
Edit: You can use this module to do what you want.
Nothing is wrong here, you just see the internal representation of strings. If you want a more readable output try one of these:
(some of them might only work in SWI-Prolog, but you have tagged it as SWI, so I think that's no problem)
use name/2
to convert from Number-Lists to atom:
?- name(X, "hallo").
X=hallo
?- answer(X), name(Y, X).
X = [89, 101, 115],
Y = 'Yes' ;
use format/2
for output.
format('~s',["hallo"]).
hallo
true.
?- answer(X), format('answer is "~s"',[X]).
answer is "Yes"
X = [89, 101, 115] ;
answer is "No"
X = [78, 111].
or, if you didn't want to use real strings (codepoint lists) use single quotes:
answer('yes').
answer('no').
answer('Variable = value').
?-answer(X).
yes;
…
精彩评论