Exercise of Programming with Mathematica

*To*: mathgroup at smc.vnet.net*Subject*: [mg131855] Exercise of Programming with Mathematica*From*: Zhenyi Zhang <infozyzhang at gmail.com>*Date*: Fri, 18 Oct 2013 04:45:28 -0400 (EDT)*Delivered-to*: l-mathgroup@mail-archive0.wolfram.com*Delivered-to*: l-mathgroup@wolfram.com*Delivered-to*: mathgroup-outx@smc.vnet.net*Delivered-to*: mathgroup-newsendx@smc.vnet.net

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.

**Follow-Ups**:**Re: Exercise of Programming with Mathematica***From:*Itai Seggev <itais@wolfram.com>

**Re: Exercise of Programming with Mathematica***From:*Adriano Pascoletti <adriano.pascoletti@uniud.it>

**Re: Exercise of Programming with Mathematica***From:*"Harvey P. Dale" <hpd@hpdale.org>

**Re: Exercise of Programming with Mathematica***From:*Sseziwa Mukasa <mukasa@gmail.com>