在Prolog中,如何将查询的所有答案转换为列表?

时间:2019-07-25 22:15:22

标签: prolog swi-prolog

我只是从Prolog开始。

并使用网络服务器功能。

我想要一个页面,该页面列出了数据库中符合某个谓词的所有事物。

如何将谓词查询转换为列表?

或者如果存在无限数量的结果的危险,我该如何做“取n”的等价物?

1 个答案:

答案 0 :(得分:0)

SWI-Prolog提出了一种特殊的HTML生成实用程序,基于DCG。然后,您可能会对库(dcg/high_order)感兴趣,有可能与库(solution_sequences)混合使用,在其中您发现可以减少使用SWI-Prolog时遇到的SQL“阻抗不匹配”的谓词。 -更常见-开发经验。

让我们尝试为库(this documentation sample)改编http/html_write,将apropos_rows // 1重构为apropos_row // 1,执行真正需要的格式化,并调用sequence // 2:< / p>

apropos_page(Kwd, Matches) -->
        page([ title(['Predicates for ', Kwd])
             ],
             [ h2(align(center),
                  ['Predicates for ', Kwd]),
               table([ align(center),
                       border(1),
                       width('80%')
                     ],
                     [ tr([ th('Predicate'),
                            th('Summary')
                          ])
                     | \sequence(apropos_row, Matches)
                     ])
             ]).

apropos_row(pred(Name, Arity, Summary)) -->
        html([ tr([ td(\predref(Name/Arity)),
                    td(em(Summary))
                  ])
             ]).