22
$\begingroup$

This question already has an answer here:

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.

$\endgroup$

marked as duplicate by rm -rf Jul 28 '13 at 7:09

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ @kguler but HoldFirst is not a "held expression" wrapper, it's an attribute. $\endgroup$ – FJRA Mar 7 '12 at 2:08
  • $\begingroup$ @FJRA, just realized why OP wanted to use Hold rather than HoldFirst or HoldRest or HoldAll (and deleted my previous comment before I saw your explanation) $\endgroup$ – kglr Mar 7 '12 at 2:14
  • $\begingroup$ @rm-rf & WReach, is there any particular reason why 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$ – Kuba Nov 22 '13 at 9:47
  • $\begingroup$ @Kuba Verbatim is only used becase I wanted to pass on arguments as Sequence[5, 6] instead of 5|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$ – István Zachar Nov 22 '13 at 9:59
  • $\begingroup$ Ok, thanks, I just wasn't sure if I get everything :) $\endgroup$ – Kuba Nov 22 '13 at 10:07
24
$\begingroup$

Here are a couple of alternatives to Trott-Strzebonski in @R.M's answer:

Hold[{3,4,5|6}] /.
  Verbatim[Alternatives][x__] :> RuleCondition@RandomChoice@List@x

Hold[{3, 4, 5}]

Hold[{3,4,5|6}] /.
  Verbatim[Alternatives][x__] :> Block[{}, RandomChoice@List@x /; True]

Hold[{3, 4, 6}]

They operate on the same principle as Trott-Strzebonski (i.e. RuleCondition), but express the effect in different ways.

$\endgroup$
  • 1
    $\begingroup$ I was tempted to post this answer, but decided to wait for you, as the proper one to do it :). +1. $\endgroup$ – Leonid Shifrin Mar 7 '12 at 6:44
22
$\begingroup$

This is a case where the Trott-Strzebonski in-place evaluation trick is useful. You use With to inject inside your held expression as:

(Hold[{3, 4, 5 | 6}] /. (Verbatim@Alternatives)[x__] :> 
    With[{eval = RandomChoice@List@x}, eval /; True])

Out[1]= Hold[{3, 4, 5}]

You should definitely read this post by Leonid, that gives you a good insight into how this works, but in short, using Condition or /; forces the evaluation of eval when the condition is True (i.e., always) and then injected arbitrarily deep using With.

$\endgroup$
  • $\begingroup$ Just a note the talk title is "Working with Unevaluated Expressions" in case the link to wolfram library changes again in the future. $\endgroup$ – xslittlegrass Jul 19 '15 at 16:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.