【问题标题】:Why am I getting an existence error on predsort?为什么我在 predsort 上出现存在错误?
【发布时间】:2013-10-27 01:15:41
【问题描述】:

我正在尝试按完成路径所需的距离对路径列表进行排序。我正在使用的 Prolog 代码如下。

当我调用sortRoutes 时,我从Prolog 收到一个存在错误,说predsort 不存在。但是,我正在使用排序模块,这似乎并没有改变任何东西。

我似乎无法弄清楚为什么这不起作用。我做错了什么吗?

谢谢!

:- use_module(library(sort)).

sortRoutes(DistRoutes, SortedRoutes) :-
    predsort(distCompare, DistRoutes, SortedRoutes).

distCompare(Comp, E1, E2) :-
    my_nth(2, E1, Dist1),
    my_nth(2, E2, Dist2),
    compDists(Dist1, Dist2).

compDists(>, Dist1, Dist2) :-
    Dist1 > Dist2.
compDists(<, Dist1, Dist2) :-
    Dist1 =< Dist2.

【问题讨论】:

  • 你的意思是:- use_module(library(sort)).
  • 是的,对不起。即使在适当的位置它也不起作用。
  • 另外我假设您的compDists(Dist1, Dist2) 实际上应该是compDists(Comp, Dist1, Dist2)?有了这个改变,我在 SWI Prolog 中运行你的代码就好了。
  • 我尝试了您的建议,但仍然出现此错误:uncaught exception: error(existence_error(procedure,predsort/3),sortRoutes/2)。这个存在错误实际上是想说predsort/sortRoutes 不存在还是在这种情况下存在错误意味着其他什么?
  • 它抱怨sortRoutes/2 不存在。正如我所提到的,在我将Comp 参数添加到compDists 调用并编造了几个my_nth 事实之后,上面的代码对我有用。您发布的内容与实际运行的内容有什么不同吗?

标签: sorting module comparison prolog swi-prolog


【解决方案1】:

我认为 distCompare 应该是

distCompare(Comp, E1, E2) :-
    my_nth(2, E1, Dist1),
    my_nth(2, E2, Dist2),
    compDists(Comp, Dist1, Dist2).

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-03
    • 2019-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-08
    • 2017-12-04
    • 2014-05-23
    相关资源
    最近更新 更多