SWI-Prolog 如何显示整个答案(列表)?

SWI-Prolog how to show entire answer (list)?

我正在尝试将字符串转换为 ascii 码列表,如下所示:

7 ?- string_to_list("I'm a big blue banana in space!", C).
C = [73, 39, 109, 32, 97, 32, 98, 105, 103|...].

8 ?- 

如您所见,这并没有给我完整的列表,但我需要它。

This solution does not work: I can't press w since it gives me the answer and does a full stop. Neither does this: 我可以调用这个函数了,它returns正确,但列表仍然没有完全显示。

11 ?- set_prolog_flag(toplevel_print_options,[quoted(true), portray(true), max_depth(0), spacing(next_argument)]).
true.

12 ?- string_to_list("I'm a big blue banana in space!", C).
C = [73, 39, 109, 32, 97, 32, 98, 105, 103|...].

13 ?- 

感谢任何帮助!

?- set_prolog_flag(answer_write_options,[max_depth(0)]).
true.

?- string_to_list("I'm a big blue banana in space!", C).
C = [73,39,109,32,97,32,98,105,103,32,98,108,117,101,32,98,97,110,97,110,97,32,105,110,32,115,112,97,99,101,33].

这里应该有相同的答案...我使用的是最新的 SWI-prolog 版本,刚刚编译...

只是把 放在答案中:

?- string_codes("string_to_list/2 is deprecated; use string_codes/2!", Codes)
   ;
   false.
Codes = [115, 116, 114, 105, 110, 103, 95, 116, 111|...] /* press w */ [write]
Codes = [115, 116, 114, 105, 110, 103, 95, 116, 111, 95, 108, 105, 115, 116, 47, 50, 32, 105, 115, 32, 100, 101, 112, 114, 101, 99, 97, 116, 101, 100, 59, 32, 117, 115, 101, 32, 115, 116, 114, 105, 110, 103, 95, 99, 111, 100, 101, 115, 47, 50, 33] /* press enter */.

但是,从这里开始所有项将被完整显示。这可能会很烦人。

您还可以使用其中一种打印谓词:

?- ..., writeln(Codes).

但出于某种原因,这是不受欢迎的。如果您在一个答案中报告了多个绑定,这绝对有用,但您只想查看其中一个变量的完整值:

?- numlist(1,1000,L),
   Codes = `This is a code list in SWI-Prolog V7`.
L = [1, 2, 3, 4, 5, 6, 7, 8, 9|...],
Codes = [84, 104, 105, 115, 32, 105, 115, 32, 97|...].

?- numlist(1,1000,L),
   Codes = `This is a code list in SWI-Prolog V7`,
   writeln(Codes).
[84,104,105,115,32,105,115,32,97,32,99,111,100,101,32,108,105,115,116,32,105,110,32,83,87,73,45,80,114,111,108,111,103,32,86,55]
L = [1, 2, 3, 4, 5, 6, 7, 8, 9|...],
Codes = [84, 104, 105, 115, 32, 105, 115, 32, 97|...].

我找到了两种方法。


1.

?- set_prolog_flag(answer_write_options,[max_depth(0)]).
true.

然后执行打印截断列表的命令。

(set_prolog_flag documentation)


2.

?- atom_chars(goodbye_prolog, X) ; true.

(AllOutput documentation)

; true. 放在导致长列表的调用末尾。然后按键盘上的 w 键。结果是:

?- sudoku([_,_,2,3,_,_,_,_,_,_,_,_,3,4,_,_], Solution); true.
Solution = [4, 1, 2, 3, 2, 3, 4, 1, 1|...] [write]
Solution = [4, 1, 2, 3, 2, 3, 4, 1, 1, 2, 3, 4, 3, 4, 1, 2] ;
true.

如果您发现自己经常使用 string_codes/2atom_codes/2,请重新考虑您的方法。您可以使用 chars 代替 codes 并完全避免 SWI 特定的字符串数据类型。所有这一切,通过设置 Prolog 标志:

?- set_prolog_flag(double_quotes, chars).
true.

?- Chs = "Codes are unreadable!".
Chs = ['C', o, d, e, s, ' ', a, r, e|...].

这比[67, 111, 100, 101, 115, 32, 97, 114, 101|...]更具可读性,但仍然没有解决您的问题。但是,您现在可以使用 library(double_quotes).

紧凑地显示此类答案
?- use_module(double_quotes).
true.

?- Chs = "Codes are unreadable!".
Chs = "Codes are unreadable!".

?- Chs = "Codes are unreadable", Chs = [C|Chs2].
Chs = "Codes are unreadable",
C = 'C',
Chs2 = "odes are unreadable".

使用此设置,您可以更方便地处理文本。它也非常适合 DCG。

你可以用这个解决你的问题,

?- set_prolog_flag(answer_write_options,[max_depth(0)]).
true.

但是,如果您打算始终显示所有结果而不被列入候选名单,那么您可以在 "init".

中添加此行

导航: SWI-Prolog >设置 > 用户初始化文件

如果您从未创建过文件,系统会要求您创建一个 swipl.ini 文件。

总之,以后再启动prolog时,就不需要再输入上面这行代码了。