Search code examples
prologswapslash

swap function in prolog


I need to replace the term A/B in prolog with B/A.

A and B are variables (could be either a number, an uppercase letter, or a lowercase letter)

I mean, what is on the right side of the slash should be on the left side and vice versa.

Examples:
A/10 should be 10/A
9/3 should be 3/9
5/B should be B/5

I hope it is clear. thanks.


Solution

  • really simple:

    swap(X/Y, Y/X).
    

    or could use unification 'inline', here a sample:

    ?- forall(member(A, [a/3, b/6]), (A=X/Y, B=Y/X, writeln(B))).
    3/a
    6/b
    true.