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?
{a, b, c, d, e, f} //. {r___, Except[1], y___} -> {r, 1, y}produces{1, 1, 1, 1, 1, 1}– m_goldberg Mar 21 '15 at 14:26