Sure. So if we look at your formula (other than the first row = 0)
If( Mod( Lag( :RandomList ), 2 ) == 0,
:RandomList[Row()] + 1,
:RandomList[Row()] - 1
)
I see it as 3 parts, the lag, the mod == 0 check of the lag, and the else expression of that.
So I first do the lag
lv = lag_matrix(v, 1);
then I get the mod vector
mod_vector = lv / 2 - floor(lv/2);
but I just realized mod works on vectors so you could do
mod_vector = mod(lv, 2);
which is significantly faster
then I get a boolean vector by doing
mod_vector == 0 and loc() it to find the rows where the mod(2) of the lag(1) is 0
loc_vector = loc(mod_vector==0);
loc_vector is the rows that match your if statement
Mod( Lag( :RandomList ), 2 ) == 0
then to get the else statement rows, I invert the selection by making a vector of all 1s and setting the loc_vector rows to 0 and doing a loc()
invert_vector = J(nrows(v), 1);
invert_vector[loc_vector] = 0;
invert_vector = loc(invert_vector);
so if you concatted the two vectors and sorted, it should just be 1::nrows()
after that I just set the rows for the if expression to +1 and the else expression to -1
v[loc_vector] = v[loc_vector] + 1;
v[invert_vector] = v[invert_vector] - 1;
and finally set the first row to 0
v[1] = 0;