Logical variables in Prolog

Submitted by Greg Buchholz
on June 22, 2009 - 8:55pm

I came across this code, which was supposed to demonstrate a Prolog program that couldn't be written in Mercury. I couldn't help but rewrite that as:

rank(Xs,Rs) :- pairs_keys_values(Decorated,Xs,Rs),
               keysort(Decorated,Sorted),
               numerate(Sorted,1).

numerate([],_).
numerate([_-N|Rest],N) :- N1 is N + 1, numerate(Rest,N1).

...This computes the rank order of a list of elements. For example rank([40,20,50,10],L). unifies L with [3,2,4,1], since 40 is the third largest element, etc.