Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #16073

Exercise of Programming with Mathematica

From Zhenyi Zhang <infozyzhang@gmail.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Exercise of Programming with Mathematica
Date 2013-10-18 08:32 +0000
Message-ID <l3qrmm$3ps$1@smc.vnet.net> (permalink)
Organization Time-Warner Telecom

Show all headers | View raw


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.

Back to comp.soft-sys.math.mathematica | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Exercise of Programming with Mathematica Zhenyi Zhang <infozyzhang@gmail.com> - 2013-10-18 08:32 +0000
  Re: Exercise of Programming with Mathematica Helen Read <readhpr@gmail.com> - 2013-10-21 03:29 +0000
  Re: Exercise of Programming with Mathematica Douglas Skinner <d_dubya_skinner@hotmail.com> - 2013-11-06 05:21 +0000

csiph-web