The title might be fuzzy, just see the code
number(one, odd).
number(two, even).
number(three, odd).
number(four, even).
number(five, odd).
greaterThan(five, four).
greaterThan(four, three).
greaterThan(three, two).
greaterThan(two, one).
if_larger(A,B):- elder_than(A,B).
if_larger(A,B):- elder_than(A,X),greaterThan(X,B).
even(A):- number(A, even).
odd(A):- number(A, odd).
largest(A):-
not(greaterThan(_,A)).
largestEven(A):-
even(A),
not((if_larger(X,A), even(X))).
largestOdd(A):-
odd(A),
not((if_larger(X,A), odd(X))).
how to sort the numbers in the following order: one, three, five, two, four.
I think the solution should be in the following form, however I couldn't figure them out.
next(A, Next):-
odd(A), odd(Next),
...
next(A, Next):-
even(A), even(Next),
...
next(A, Next):-
odd(A), even(Next),
...
Or, is it possible to generate a list, like [one, three, five, two, four].
I will answer my own question. The idea is using final/3 to construct a list of all the numbers, then apply insertion sort on the numbers list.
Implementation
Result
This question is actually a modified version of my school assignment. The original question is Royal Family Succession in Prolog. I have asked a lot of friends on how to solve this problem, and finally got this solution. I will post the original question here as well.