【问题标题】:SWI-Prolog - show long listSWI-Prolog - 显示长列表
【发布时间】:2012-01-04 03:06:34
【问题描述】:

我正在使用 SWI-Prolog,我正在尝试打印一个列表,但如果该列表有超过 9 个项目 - 看起来像这样 -

[1, 15, 8, 22, 5, 19, 12, 25, 3|...] 

有没有办法显示整个列表?

【问题讨论】:

    标签: swi-prolog prolog-toplevel


    【解决方案1】:

    看看:http://www.swi-prolog.org/FAQ/AllOutput.html

    简单的解决方法是在给出答案后输入w,即:

    ?- n_queens_problem(10,X).
    X = [1, 3, 6, 8, 10, 5, 9, 2, 4|...] [write]
    X = [1, 3, 6, 8, 10, 5, 9, 2, 4, 7] 
    

    按下“w”键后,“[write]”会显示在最后,完整的解决方案会出现在下一行。

    【讨论】:

    • 如果 prolog 只返回一个答案,这将不起作用。
    • 如果查询确定成功,您可以简单地写:?- solution(S) ; true.,即,只需附加; true 以引入选择点。然后,您还有机会按w
    • 你只需在输出中输入 w
    【解决方案2】:

    我找到了两种方法。


    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.
    

    【讨论】:

      【解决方案3】:

      如果 prolog 只返回一个答案,您可以通过键入“; true”让它等待。在谓词之后。然后,如果您按“w”,您将看到文档中所写的整个列表:http://www.swi-prolog.org/FAQ/AllOutput.html

      【讨论】:

        【解决方案4】:
        ?- createListSomehow(List), print(List), nl.
        

        会做得足够整齐。我就是这么做的。

        变化:

        ?- use_module(library(pprint)). %load a library to do pretty-printing
        ?- createListSomehow(List), print_term(List,[]), nl.
        

        print_term[] 参数是一个(空)选项列表。欲了解更多信息,see documentation

        【讨论】:

          【解决方案5】:

          如果您希望 SWI-Prolog 默认显示整个列表,您可以将此行添加到您的 init file

          :- set_prolog_flag(answer_write_options,[max_depth(0)]).
          

          您可以从 GUI 轻松修改初始化文件(设置 => 用户初始化文件)。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-12-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多