.如何在Prolog中的写入谓词中写入单引号( ' )?

发布于 2024-10-06 04:40:53 字数 385 浏览 0 评论 0原文

我想在程序的开头添加简短的信息,我就像这样

message :-
    nl,nl,
    write('  To start type  '), nl,
    write(' ?- solve(Input1,Input2,Output3) '), nl.
:- message.

这很好......但我需要 write(' ?-solve('Input1','Input2',Output3) '), nl 所以当我运行程序时它应该打印 开始输入 ?- 解决(' 输入1 ' ,' 输入2 ' ,Output3 )

提前感谢:)

I want to add short info in the beginning of the program and i goes like this

message :-
    nl,nl,
    write('  To start type  '), nl,
    write(' ?- solve(Input1,Input2,Output3) '), nl.
:- message.

And this is fine...but i need write(' ?- solve('Input1','Input2',Output3) '), nl
so when i run the program it should print
To start type
?- solve(' Input1 ' ,' Input2 ' ,Output3 )

thanks in advance :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

不再见 2024-10-13 04:40:53

使用反斜杠转义引号。

例如,要输出一个单引号:

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

作为一般规则,您当然应该完全避免副作用完全。一个好的解决方案是使用 DCG 来描述输出。这使得它适合测试用例,如果输出仅出现在终端上,则很难编写测试用例。

write/1 特别很少使用。如果您确实需要输出某些内容,请使用format/2。如果您了解 DOS,这听起来很可怕,但事实并非如此。

format/2 的一个重要优点是,它可以让您方便地将静态文本与灵活的参数进行网格化,例如:

?- member(X, [friend,foe,love]),
   format("hello my '~q'!\n", [X]),
   false.

Yielding:

hello my 'friend'!
hello my 'foe'!
hello my 'love'!

请注意,在这种情况下甚至没有出现单引号的问题。 " 的类似问题可以通过使用 \ 再次解决:

?- format("a \"test\"", []).
a "test"

Escape the quote with backslash.

For example, to output a single single-quote:

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

As a general rule, you should of course avoid side-effects entirely. A good solution is to describe the output using a DCG. This makes it amenable to test cases, which are hard to write if output only appears on the terminal.

write/1 is particularly rarely used. If you really need to output something, use format/2. This sounds scary if you know DOS, but it really isn't.

An important advantage of format/2 is that it lets you conveniently mesh static text with flexible arguments, for example:

?- member(X, [friend,foe,love]),
   format("hello my '~q'!\n", [X]),
   false.

Yielding:

hello my 'friend'!
hello my 'foe'!
hello my 'love'!

Note that the issue of single quotes didn't even arise in this case. The analogous issue with " can again be solved by using \:

?- format("a \"test\"", []).
a "test"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文