       RE: Replacement Rule

• To: mathgroup at smc.vnet.net
• Subject: [mg32084] RE: [mg32064] Replacement Rule
• From: "David Park" <djmp at earthlink.net>
• Date: Sat, 22 Dec 2001 04:23:02 -0500 (EST)
• Sender: owner-wri-mathgroup at wolfram.com

```Brian,

The second example is certainly correct because the rule picks the first of
two items in a list of two lists. If the lists you are dealing with always
contains numbers, you could use a more specific rule.

l1 = {{1, 2}, {3, 4}, {5, 6}};
l2 = {{3, 4}, {5, 6}};

rule = {(x_)?NumberQ, (y_)?NumberQ} -> x;

l1 /. rule
l2 /. rule
{1, 3, 5}
{3, 5}

If your lists contain expressions or symbols you could use a more general
set of rules which handles the special case first.

rules = {{{a_, b_}, {c_, d_}} -> {a, c}, {a_, b_} -> a}

l1 /. rules
l2 /. rules
{1, 3, 5}
{3, 5}

{{a1, Sin[x]}, {b1, Cos[x]}, {c1, a + b}} /. rules
{a1, b1, c1}

{{a1, Sin[x]}, {b1, Cos[x]}} /. rules
{a1, b1}

David Park

> From: Brian Higgins [mailto:bghiggins at ucdavis.edu]
To: mathgroup at smc.vnet.net
>
> I have the following list
>
> l1={{1,2},{3,4},{5,6}};
>
> and then I operate on it with the following pattern/replacement rule
>
> In:= l1 /. {x_, y_} -> x
>
> Out= {1, 3, 5}
>
> This is what I would expect. Now consider the following list
>
> In:= l2 = {{3, 4}, {5, 6}};
>
> Then I use the same pattern/replacement rule
>
> In:=l2 /. {x_, y_} -> x
>
> Out= {3, 4}
>
> I was hoping to get
>
> {3,5}
>
> What am I missing? The FullForm of l2 is basically the same structure
> as l1, as far as I can tell....
>
> Brian
>

```

• Prev by Date: RE: Replacement Rule
• Next by Date: Re: Replacement Rule
• Previous by thread: RE: Replacement Rule
• Next by thread: Re: Replacement Rule