I know that there are methods to structurally manipulate held expressions (discussed e.g. here), but I failed to apply those for this particular problem:
(Hold[{3, 4, 5 | 6}] /. (Verbatim@Alternatives)[x__] :> RandomChoice@List@x)
Hold[{3, 4, RandomChoice[{5, 6}]}]
The code should replace any Alternatives
in the held expression with an appropriate choice from the alternatives, in this case either 5 or 6, i.e. it should evaluate the replacement.
HoldFirst
is not a "held expression" wrapper, it's an attribute. $\endgroup$Hold
rather thanHoldFirst
orHoldRest
orHoldAll
(and deleted my previous comment before I saw your explanation) $\endgroup$Verbatim
is used in each answer? This will do too:/. x_Alternatives :> RuleCondition@RandomChoice@(List @@ x)
or/. x_Alternatives :> With[{eval = RandomChoice@(List @@ x)}, eval /; True])
. $\endgroup$Verbatim
is only used becase I wanted to pass on arguments asSequence[5, 6]
instead of5|6
as in a replacement, the latter could possibly match more than one thing. It's not necessary but I guess they all tried to comply with my original specification. Your's is equally good. $\endgroup$