MathGroup Archive 2013

[Date Index] [Thread Index] [Author Index]

Search the Archive

Exercise of Programming with Mathematica


Here is a rule designed to switch the order of each pair of expressions in a list. It works fine on the
first example, but fails on the second.
In[1]:= {{a, b}, {c, d}, {e, f}}/.{x_, y_} :> {y, x}
Out[1]= {{b, a}, {d, c}, {f, e}}
In[2]:= {{a, b}, {c, d}}/.{x_, y_} :> {y, x}
Out[2]= {{c, d}, {a, b}}
Explain what has gone wrong and rewrite this rule to correct the situation, that is, so that the second
example returns {{b, a}, {d, c}}

My solution is the most stupid one.

{{a, b}, {c, d}}/.{{x_, y_}, {w_, t_}} :> {{y, x}, {t, w}}

May I ask any elegant solutions?

Thanks.



  • Prev by Date: A law of small sines?
  • Next by Date: Re: Version 9 Trial contaminates Version 8 running paid version
  • Previous by thread: A law of small sines?
  • Next by thread: Re: Exercise of Programming with Mathematica