Prolog 返回一个列表而不是几个可能的字符串
answer("Yes").
answer("No").
answer("Variable = value").
receive(A) :- answer(A).
2 ?- answer(A).
A = [89, 101, 115]
Yes
我想要 A = "Yes"
等。我做错了什么?
answer("Yes").
answer("No").
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您将获得字符串 Yes、No 和 Variable = value 的列表表示形式。
如果你想用 Yes、No 和 Variable = value 来实例化 A,你应该将它们括在单引号而不是双引号之间:
如果你想返回包含双引号的术语,你应该包含它们,但也将每个括起来带单引号的术语:
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:
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:
你没有做错什么。
[89, 101, 115]
与"Yes"
相同:编辑:您可以使用 此模块 来完成您想要的操作。
You are doing nothing wrong.
[89, 101, 115]
is the same as"Yes"
:Edit: You can use this module to do what you want.
这里没有什么问题,你只是看到了字符串的内部表示。
如果您想要更具可读性的输出,请尝试以下之一:(
其中一些可能仅适用于 SWI-Prolog,但您已将其标记为 SWI,所以我认为这没有问题)
使用
name/2
从数字列表转换为原子:使用
format/2
进行输出。或者,如果您不想使用真正的字符串(代码点列表),请使用单引号:
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:use
format/2
for output.or, if you didn't want to use real strings (codepoint lists) use single quotes: