I have a list of integers; I would like to replace anything of the form $x,y,z,2$ in such a list with $x+1, y+1, 0, 0$. The problem is that the $2$ may appear at the front, or in the first three elements, of the sequence (in which case I want to just pretend that the missing leading elements are zero), so I'm trying to use optional pattern values to deal with that. Here is what I've tried (with a different pattern output just to figure out what is going on)
{0, 1, 1, 2} /. {x___, a_: 0, b_: 0, c_: 0, 2, y___} :> {x, a, b, c}
{{}, 0, 1, 1} (* this is what I'd expect. x matches to the null string *)
{1, 1, 2} /. {x___, a_: 0, b_: 0, c_: 0, 2, y___} :> {x, a, b, c}
{{}, 1, 1, 0} (* I would have expected a=0 and b=c=1 *)
{1, 2} /. {x___, a_: 0, b_: 0, c_: 0, 2, y___} :> {{x}, a, b, c}
{{}, 1, 0, 0} (* I would have expected a=b=0, c=1 *)
{2} /. {x___, a_: 0, b_: 0, c_: 0, 2, y___} :> {{x}, a, b, c}
{{}, 0, 0, 0} (* This matched as I would expect *)
Clearly I'm misunderstanding how this pattern works. Can someone enlighten me?
I would really like to know not only what the right way to do this is, but also why what I tried did not work. I've read the section in Shifrin's book on patterns; are there other good resources to understand how patterns work?