Post Closed as "duplicate" by Mr.Wizard pattern-matching of
4 Improved formatting
source | link

ReplaceRepeated performsmade a single replacement instead ofwhere I expected multiple replacements to be made

This seems like a very simple action: {a, b, c, d, e, f} //. {r___, x_, y___} :> {r, 1, y}

{a, b, c, d, e, f} //. {r___, x_, y___} :> {r, 1, y}

but it replaces only the first element: {1, b, c, d, e, f} giving

{1, b, c, d, e, f}

Why? what am i doing wrong? The specified pattern seems to describe every element of the list..

Clarification. I want to change all elements of the given list to 1 using ReplaceRepeatedReplaceRepeated only. For a list of symbols it is straightforward:

In[68]:=  {a, b, c, d, e, f} //. {r___, x_Symbol, y___} :> {r, 1, y}
Out[68]:= {1, 1, 1, 1, 1, 1}
{1, 1, 1, 1, 1, 1}

But what if the list has elements of different type?

And another tweak on this problem. What if i want to modify list elements using only patterns such as:

In[70]:=  someFun[n_?EvenQ] := n + 1;
          someFun[n_?OddQ] := 2*n;
          Replace[{1, 2, 3}, {r___, x_, y___} :> {r, someFun[x], y}]
Out[70]:= {2,2,3}
{2,2,3}

I could do it using

In[71]:=  Map[someFun,{1,2,3}]
Out[71]:= {2,3,6}
{2,3,6}

but still would like to do it using pattern. Is it possible?

thank you very much for your help!!!

ReplaceRepeated performs a single replacement instead of multiple

This seems like a very simple action: {a, b, c, d, e, f} //. {r___, x_, y___} :> {r, 1, y}

but it replaces only the first element: {1, b, c, d, e, f}

Why? what am i doing wrong? The specified pattern seems to describe every element of the list..

Clarification. I want to change all elements of the given list to 1 using ReplaceRepeated only. For a list of symbols it is straightforward:

In[68]:=  {a, b, c, d, e, f} //. {r___, x_Symbol, y___} :> {r, 1, y}
Out[68]:= {1, 1, 1, 1, 1, 1}

But what if the list has elements of different type?

And another tweak on this problem. What if i want to modify list elements using only patterns such as:

In[70]:=  someFun[n_?EvenQ] := n + 1;
          someFun[n_?OddQ] := 2*n;
          Replace[{1, 2, 3}, {r___, x_, y___} :> {r, someFun[x], y}]
Out[70]:= {2,2,3}

I could do it using

In[71]:=  Map[someFun,{1,2,3}]
Out[71]:= {2,3,6}

but still would like to do it using pattern. Is it possible?

thank you very much for your help!!!

ReplaceRepeated made a single replacement where I expected multiple replacements to be made

This seems like a very simple action:

{a, b, c, d, e, f} //. {r___, x_, y___} :> {r, 1, y}

but it replaces only the first element giving

{1, b, c, d, e, f}

Why? what am i doing wrong? The specified pattern seems to describe every element of the list..

Clarification. I want to change all elements of the given list to 1 using ReplaceRepeated only. For a list of symbols it is straightforward:

 {a, b, c, d, e, f} //. {r___, x_Symbol, y___} :> {r, 1, y}
{1, 1, 1, 1, 1, 1}

But what if the list has elements of different type?

And another tweak on this problem. What if i want to modify list elements using only patterns such as:

 someFun[n_?EvenQ] := n + 1;
          someFun[n_?OddQ] := 2*n;
          Replace[{1, 2, 3}, {r___, x_, y___} :> {r, someFun[x], y}]
{2,2,3}

I could do it using

 Map[someFun,{1,2,3}]
{2,3,6}

but still would like to do it using pattern. Is it possible?

3 another tweak to the problem: modifying each element of the list using pattern instead of Map
source | link

This seems like a very simple action: {a, b, c, d, e, f} //. {r___, x_, y___} :> {r, 1, y}

but it replaces only the first element: {1, b, c, d, e, f}

Why? what am i doing wrong? The specified pattern seems to describe every element of the list..

Clarification. I want to change all elements of the given list to 1 using ReplaceRepeated only. For a list of symbols it is straightforward:

In[68]:=  {a, b, c, d, e, f} //. {r___, x_Symbol, y___} :> {r, 1, y}
Out[68]:= {1, 1, 1, 1, 1, 1}

But what if the list has elements of different type?

And another tweak on this problem. What if i want to modify list elements using only patterns such as:

In[70]:=  someFun[n_?EvenQ] := n + 1;
          someFun[n_?OddQ] := 2*n;
          Replace[{1, 2, 3}, {r___, x_, y___} :> {r, someFun[x], y}]
Out[70]:= {2,2,3}

I could do it using

In[71]:=  Map[someFun,{1,2,3}]
Out[71]:= {2,3,6}

but still would like to do it using pattern. Is it possible?

thank you very much for your help!!!

This seems like a very simple action: {a, b, c, d, e, f} //. {r___, x_, y___} :> {r, 1, y}

but it replaces only the first element: {1, b, c, d, e, f}

Why? what am i doing wrong? The specified pattern seems to describe every element of the list..

Clarification. I want to change all elements of the given list to 1 using ReplaceRepeated only. For a list of symbols it is straightforward:

In[68]:=  {a, b, c, d, e, f} //. {r___, x_Symbol, y___} :> {r, 1, y}
Out[68]:= {1, 1, 1, 1, 1, 1}

But what if the list has elements of different type?

thank you very much for your help!!!

This seems like a very simple action: {a, b, c, d, e, f} //. {r___, x_, y___} :> {r, 1, y}

but it replaces only the first element: {1, b, c, d, e, f}

Why? what am i doing wrong? The specified pattern seems to describe every element of the list..

Clarification. I want to change all elements of the given list to 1 using ReplaceRepeated only. For a list of symbols it is straightforward:

In[68]:=  {a, b, c, d, e, f} //. {r___, x_Symbol, y___} :> {r, 1, y}
Out[68]:= {1, 1, 1, 1, 1, 1}

But what if the list has elements of different type?

And another tweak on this problem. What if i want to modify list elements using only patterns such as:

In[70]:=  someFun[n_?EvenQ] := n + 1;
          someFun[n_?OddQ] := 2*n;
          Replace[{1, 2, 3}, {r___, x_, y___} :> {r, someFun[x], y}]
Out[70]:= {2,2,3}

I could do it using

In[71]:=  Map[someFun,{1,2,3}]
Out[71]:= {2,3,6}

but still would like to do it using pattern. Is it possible?

thank you very much for your help!!!

2 Clarification: want to change all elements in the list into 1 using only ReplaceRepeated.
source | link

This seems like a very simple action: {a, b, c, d, e, f} //. {r___, x_, y___} :> {r, 1, y}

but it replaces only the first element: {1, b, c, d, e, f}

Why? what am i doing wrong? The specified pattern seems to describe every element of the list..

Clarification. I want to change all elements of the given list to 1 using ReplaceRepeated only. For a list of symbols it is straightforward:

In[68]:=  {a, b, c, d, e, f} //. {r___, x_Symbol, y___} :> {r, 1, y}
Out[68]:= {1, 1, 1, 1, 1, 1}

But what if the list has elements of different type?

thank you very much for your help!!!

This seems like a very simple action: {a, b, c, d, e, f} //. {r___, x_, y___} :> {r, 1, y}

but it replaces only the first element: {1, b, c, d, e, f}

Why? what am i doing wrong? The specified pattern seems to describe every element of the list..

thank you very much for your help!!!

This seems like a very simple action: {a, b, c, d, e, f} //. {r___, x_, y___} :> {r, 1, y}

but it replaces only the first element: {1, b, c, d, e, f}

Why? what am i doing wrong? The specified pattern seems to describe every element of the list..

Clarification. I want to change all elements of the given list to 1 using ReplaceRepeated only. For a list of symbols it is straightforward:

In[68]:=  {a, b, c, d, e, f} //. {r___, x_Symbol, y___} :> {r, 1, y}
Out[68]:= {1, 1, 1, 1, 1, 1}

But what if the list has elements of different type?

thank you very much for your help!!!

1
source | link