# Is there a possibility of adding a second condition, or an else clause to $/;$?

Let tally=RandomInteger[{0, 1}, 10];

In the code below, I'm trying to output a list where each component $$C_n=\sum^n_{i=1} \text{tally}_i / n$$

The problem with my code below is that when tally [[i]] == 0, it doesn't change the component value as the formula above...

j = 0;
acceptanceplot =
ReplacePart[tally , {i_} /; tally [[i]] == 1 :> (j++/i)];
ListPlot[acceptanceplot[[1 ;; Length[tally]]]]


Is there a possibility of adding a second condition, or an else clause to $$/;$$? If possible, then I would just add the action j/i

• Can you just string your clauses together with && or |? – Carl Lange Jan 27 at 17:41
• @CarlLange I'm not sure of what you're suggesting... Could you elaborate on it? – An old man in the sea. Jan 27 at 18:12
• I don't think your way of constructing the new list in place of the old is a good idea: you assume that ReplacePart scans the elements in order, which is not specified in the manual and thus could change in different circumstances (e.g. parallelization). – Roman Jan 27 at 18:54

SeedRandom[0]

tally = RandomInteger[{0, 1}, 10];

acceptanceplot = Accumulate[tally]/Range[Length[tally]];

ListPlot[acceptanceplot]


SeedRandom[0]

tally = RandomInteger[{0, 1}, 10^4];

acceptanceplot = Accumulate[tally]/Range[Length[tally]];

ListPlot[acceptanceplot]


I don't think that this necessarily the best way to solve the problem at hand, but I think I understand what the OP means in terms of adding an "else clause" to Condition (/;). Now it isn't possible to modify the condition itself, but instead you can just add an extra replacement rule that fires whenever the first one doesn't:

acceptanceplot = ReplacePart[
tally,
{
{i_} /; tally[[i]] == 1 :> j++/i,
{i_} :> j/i
}
]


If the first rule matches, that one will fire. Otherwise the less restrictive second rule will be applied.

• Many thanks Sjoerd for your answer. +1 ;) – An old man in the sea. Jan 27 at 20:02
acceptance = Rest@FoldList[Plus, 0, tally]/Range[Length[tally]]

• Accumulate[tally] is simpler than Rest@FoldList[Plus, 0, tally], see @BobHanlon above. – Roman Jan 27 at 19:26
• Thanks for the effort Roman. ;) – An old man in the sea. Jan 27 at 20:01