2

I am trying to write a Mathematica function that will take a rule such as a->b to `b->a'. Can this be done?

Yossi Gil
  • 121
  • 2

2 Answers2

3

Use a pattern to deconstruct the Rule, and reconstitute it backwards in your function body.

f[Rule[a_, b_]] := Rule[b, a]
John Doty
  • 13,712
  • 1
  • 22
  • 42
3
a -> b // Reverse

Or

(a -> b)[[{2, 1}]]

Or

 a -> b /. (x_ -> y_) -> y -> x
eldo
  • 67,911
  • 5
  • 60
  • 168