1

I would like to compare two lists considering and comparing every one of their subterms also. For example, if I have the lists [1, t1(2, t2(3), 4)] and [1, t1(2, t2(2), 4)], the result should be that the first one is greater (>) than the second one, because 3 > 2 in terms t2 (which are part of the term t1).
I thought of something like this:

cmp([1, t1(2, t2(3), 4)], [1, t1(2, t2(2), 4)], X).
X = >

So the input would be two list, and the output is a mathematical symbol indicating the relation between them.
Maybe my question is a little bit messy, but can this be done somehow?

Edit:
Sorry, I forgot to mention that I would like to compare the numbers arithmetically, all the other terms by the standard order. This is why I got stuck a little bit.

Thookes
  • 99
  • 1
  • 7
  • Is the structure of the terms always the same? Furthermore what is `X` supposed to unify with? – Willem Van Onsem Mar 26 '17 at 23:14
  • Try `X @> Y`. At least it works in the cases that you describe. – false Mar 26 '17 at 23:17
  • What do you mean by standard order? Lexicographic? And if so, does it mean the terms do not always match between the two lists? Could you include some more examples of possible inputs? – Andreina Jun 20 '18 at 02:23

1 Answers1

1

I believe what you're looking for is the compare/3 predicate [1]. It's a standard Prolog method so I just tested it on SICStus Prolog, but as you can see from [1], it's also available in swi prolog.

| ?- compare(X,[1, t1(2, t2(3), 4)], [1, t1(2, t2(2), 4)]). 
X = > ? 
yes

[1] - http://www.swi-prolog.org/pldoc/man?predicate=compare/3

Andreina
  • 148
  • 5