【问题标题】:How can i set a newline in a set string in SWI-Prolog?如何在 SWI-Prolog 的设置字符串中设置换行符?
【发布时间】:2019-06-16 15:13:41
【问题描述】:

我有这个字符串:

B='Dogs cats birds and fish'.

我需要它以如下格式显示:

Dogs,
cats,
birds,
and fish

有没有可能发生这种情况的方法?

我试过nl如: B='Dogs,nl,cats,nl,birds,nl,andfish'。

\n 如下: B='狗,\n,猫,\n,鸟,\n,和鱼'。

无济于事

B='狗猫鸟鱼'。

【问题讨论】:

  • 你能在这里展示一下你是如何使用nl\n的吗?
  • 我尝试了 nl,如:B='Dogs,nl,cats,nl,birds,nl,andfish'。和 \n 如:B='Dogs,\n,cats,\n,birds,\n,andfish'。

标签: swi-prolog


【解决方案1】:

换行符有效,你应该使用write/1 [swi-doc]将字符串打印到控制台,例如:

?- write('Dogs,\ncats,\nbirds,\nand fish').
Dogs,
cats,
birds,
and fish
true.

?- B = 'Dogs,\ncats,\nbirds,\nand fish', write(B).
Dogs,
cats,
birds,
and fish
B = 'Dogs,\ncats,\nbirds,\nand fish'.

如果你统一一个变量,Prolog 会将该变量打印为字符串文字,而不是字符串的内容

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-19
    • 1970-01-01
    • 2011-03-07
    • 2018-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多