I've been trying to sort a list of structure.
The structure is like this
% person(Name, Weight).
person(tom, 65).
person(dan, 70).
person(mike, 80).
And the list would be like this
List = [person(tom, 65), person(dan, 70), person(mike, 80)].
I want to sort the list from greatest weight to least. Like this:
SortList = [person(mike, 80), person(dan, 70), person(tom, 65)].
So far I have this:
sortListPerson([], []).
sortListPerson([person(NameP, WP)|Rest], Result):-
sortListPerson(Rest, List),
insertPerson(person(NameP, WP), List, Result).
insertPerson(person(NameP, WP), [], [person(NameP, WP)]).
insertPerson(person(NameP1, WP1), [person(NameP2, WP2)|Rest], [person(NameP1, WP1)|List]):-
integer(WP1),
integer(WP2),
WP1 @>= WP2,
insertPerson(person(NameP2, WP2), Rest, List).
insertPerson(person(NameP1, WP1), [person(NameP2, WP2)|Rest], [person(NameP2, WP2)|List]):-
integer(WP1),
integer(WP2),
WP1 @< WP2,
insertInPlace(person(NameP1, WP1), Rest, List).
I've tried with a list of two persons and it works:
?- sortListPerson([person(a, 10), person(b, 30)], SortList).
SortList = [person(b,30),person(a,10)] ? ;
But when I try with a list of 3 or more person appears an error:
?- sortListPerson([person(a, 10), person(b, 30), person(c, 40)], SortList).
{ERROR: arithmetic:>=/2 - expected an arithmetically evaluable expression, found person(a,10)}
no
?-
Can anybody help?
person(Name, Weight).written as a fact, do you? - lurker>=/2being used anywhere in the code you show, but the error is clearly regarding this operator. Perhaps there's an issue in yourinsertInPlace/3predicate, which isn't shown. - lurker