Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #16089
| From | Adriano Pascoletti <adriano.pascoletti@uniud.it> |
|---|---|
| Newsgroups | comp.soft-sys.math.mathematica |
| Subject | Re: Exercise of Programming with Mathematica |
| Date | 2013-10-21 03:17 +0000 |
| Message-ID | <l426bl$ldr$1@smc.vnet.net> (permalink) |
| References | <20131018084528.A030C6A21@smc.vnet.net> |
| Organization | Time-Warner Telecom |
In the second example the whole expression is matched by /. (ReplaceAll): x
is the first pair, y the second one.
Use Replace and specify the level
In[4]:= Replace[{{a, b}, {c, d}}, {x_, y_} :> {y, x}, {1}]
Out[4]= {{b, a}, {d, c}}
In[5]:= Replace[{{a, b}, {c, d}}, L : {_, _} :> Reverse[L], {1}]
Out[5]= {{b, a}, {d, c}}
Adriano Pascoletti
2013/10/18 Zhenyi Zhang <infozyzhang@gmail.com>
> 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 | Find similar | Unroll thread
Re: Exercise of Programming with Mathematica Adriano Pascoletti <adriano.pascoletti@uniud.it> - 2013-10-21 03:17 +0000
csiph-web