Prolog 返回一个列表而不是几个可能的字符串

发布于 2024-11-16 11:36:36 字数 194 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自控 2024-11-23 11:36:36

您将获得字符串 Yes、No 和 Variable = value 的列表表示形式。

如果你想用 Yes、No 和 Variable = value 来实例化 A,你应该将它们括在单引号而不是双引号之间:

answer('Yes').
answer('No').
answer('Variable = value').

如果你想返回包含双引号的术语,你应该包含它们,但也将每个括起来带单引号的术语:

answer('"Yes"').
answer('"No"').
answer('"Variable = value"').

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"').
天荒地未老 2024-11-23 11:36:36

你没有做错什么。 [89, 101, 115]"Yes" 相同:

2 ?- [89, 101, 115] = "Yes".
true.

编辑:您可以使用 此模块 来完成您想要的操作。

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.

眼前雾蒙蒙 2024-11-23 11:36:36

这里没有什么问题,你只是看到了字符串的内部表示。
如果您想要更具可读性的输出,请尝试以下之一:(

其中一些可能仅适用于 SWI-Prolog,但您已将其标记为 SWI,所以我认为这没有问题)

使用 name/2从数字列表转换为原子:

?- name(X, "hallo").
X=hallo

?- answer(X), name(Y, X).
X = [89, 101, 115],
Y = 'Yes' ;

使用 format/2 进行输出。

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].

或者,如果您不想使用真正的字符串(代码点列表),请使用单引号:

answer('yes').
answer('no').
answer('Variable = value').

?-answer(X).
yes;
…

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;
…
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文