草莓序言集

发布于 2024-12-11 02:09:54 字数 299 浏览 0 评论 0原文

我正在使用 Strawberry Prolog(我学校的计算机实验室拥有的)实现一个家谱,并且希望摆脱重复的答案。 findall 函数在 Strawberry Prolog 中工作,它将所有答案放在一个列表中,但我相信此编译器不存在 setof。我可以切换编译器或将 setof-function 添加到这个编译器中。我认为后者会是更好的学习体验,但我不知道从哪里开始。

有谁知道其他编译器中 setof 函数背后的代码以及如何将其转换为 Strawberry Prolog 吗?或者草莓 Prolog 中已经有类似 setof 的函数了吗?

谢谢。

I am implementing a family tree using Strawberry Prolog (what the computer labs have at my school) and would like get rid of the duplicate answers. The findall function works in Strawberry Prolog which put all the answers in a list, but I believe setof does not exist with this compiler. I could either switch compilers or add the setof-function to this one. I think the latter would be a better learning experience, but I have no idea where to start.

Does anyone happen to know the code behind the setof function in other compilers and how to convert this to Strawberry Prolog? Or is there a function similar to setof already in Strawberry Prolog?

Thanks.

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

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

发布评论

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

评论(1

后知后觉 2024-12-18 02:09:54

您可以简单地对 findall 的输出进行排序。当然,实现 setof 是一条更有价值的道路,但绝对不简单。这里来自 SWI-Prolog 的来源:

%%      setof(+Var, +Goal, -Set) is semidet.
%
%   Equivalent to bagof/3, but sorts the   resulting bag and removes
%   duplicate answers. We sort  immediately   after  the  findall/3,
%   removing duplicate Templ-Answer pairs early.

setof(Templ, Goal0, List) :-
    '$free_variable_set'(Templ^Goal0, Goal, Vars),
    (   Vars == v
    ->  findall(Templ, Goal, Answers),
        Answers \== [],
        sort(Answers, List)
    ;   findall(Vars-Templ, Goal, Answers),
        (   ground(Answers)
        ->  sort(Answers,Sorted),
        pick(Sorted,Vars,List)
        ;   bind_bagof_keys(Answers,_VDict),
        sort(Answers, Sorted),
        pick(Sorted, Vars, Listu),
        sort(Listu,List) % Listu ordering may be nixed by Vars
        )
    ).

如你所见,基本的它总是 findall...

You can simply sort the output of findall. Of course implementing setof it's a much more rewarding path, but definitely not simple. Here source from SWI-Prolog:

%%      setof(+Var, +Goal, -Set) is semidet.
%
%   Equivalent to bagof/3, but sorts the   resulting bag and removes
%   duplicate answers. We sort  immediately   after  the  findall/3,
%   removing duplicate Templ-Answer pairs early.

setof(Templ, Goal0, List) :-
    '$free_variable_set'(Templ^Goal0, Goal, Vars),
    (   Vars == v
    ->  findall(Templ, Goal, Answers),
        Answers \== [],
        sort(Answers, List)
    ;   findall(Vars-Templ, Goal, Answers),
        (   ground(Answers)
        ->  sort(Answers,Sorted),
        pick(Sorted,Vars,List)
        ;   bind_bagof_keys(Answers,_VDict),
        sort(Answers, Sorted),
        pick(Sorted, Vars, Listu),
        sort(Listu,List) % Listu ordering may be nixed by Vars
        )
    ).

As you can see, the basic it's always findall...

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