问题
I am trying to sort a list of paths by the distance it takes to complete them. The Prolog code I'm using is below.
When I call sortRoutes
, I get an existence error from Prolog saying that predsort
doesn't exist. However, I am using the sort module and that doesn't seem to change anything.
I can't seem to figure out why this isn't working. Am I doing anything wrong?
Thanks!
:- 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.
回答1:
I think distCompare should be
distCompare(Comp, E1, E2) :-
my_nth(2, E1, Dist1),
my_nth(2, E2, Dist2),
compDists(Comp, Dist1, Dist2).
来源:https://stackoverflow.com/questions/19613641/why-am-i-getting-an-existence-error-on-predsort