Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
@kguler but HoldFirst is not a "held expression" wrapper, it's an attribute. – FJRA Mar 7 '12 at 2:08
@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) – kguler Mar 7 '12 at 2:14

2 Answers

up vote 13 down vote accepted

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.

share|improve this answer
I was tempted to post this answer, but decided to wait for you, as the proper one to do it :). +1. – Leonid Shifrin Mar 7 '12 at 6:44

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.

share|improve this answer
While the original Trott-Strzebonski method rules, I preferred WReach's answer because of the multiple solutions and pure reputation reasons. – István Zachar Mar 7 '12 at 11:05
@IstvánZachar No worries. What did you mean by "pure reputation reasons" though? – rm -rf Mar 7 '12 at 11:09
That WReach has less rep than you and I followed the Robinhoodian line instead of the Dennismoorian. – István Zachar Mar 7 '12 at 11:17
3  
@Istvan It does not matter much here, because both answers are excellent, but generally I would think that the choice of the best answer should have nothing at all to do with the rep. I would rather use these criteria: 1. How well does the answer serve your personal needs, which prompted you to ask the question 2. How well will others coming with similar questions be served by the answer 3. How well does the answer explain the mechanisms used in it, to help people understand rather than blindly memorize. But it's just my opinion, and entirely your choice of course. – Leonid Shifrin Mar 7 '12 at 12:16
@LeonidShifrin Agreed, but if all else is equal (like in this case, as you said, both are excellent), then I use the reputation as a heuristic. Pity one cannot accept two solutions - I saved them both to a notebook for reference. – István Zachar Mar 7 '12 at 12:23
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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