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

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am writing a function that constructs a rule which has Unique[] in the RHS. Here is a simplified segment of code

$userInt = 3;

With[{x = Table[Unique[], {i, 1, 3}]},
  rules = {patt :> x}
]

Here, rules should be look like a list of Unique[], as in

patt :> {Unique[], Unique[], ...}

But the problem is that Unique[] is already evaluated by the With:

rules

(* {patt :> {$48, $48, $48}} *)

How can I generate a list of Unique[] that is not evaluated, but evaluated when it is time to pattern match?

share|improve this question
    
I messed up the code a bit. The upper limit of the Table iterator should be $userInt, not harded-coded 3. – QuantumDot Jan 12 at 14:07
up vote 3 down vote accepted
With[{x = Table[HoldForm@Unique[], {i, 1, 3}]}, rules = {patt :> x}]

enter image description here

patt /. (ReleaseHold@rules)

enter image description here

share|improve this answer

This can be a good case for a Block trick:

Block[{Unique},
  With[{x = Table[Unique[], {i, 1, 3}]}, rules = {patt :> x}]
]

(* {patt :> {Unique[], Unique[], Unique[]}} *)
share|improve this answer
$userInt = 5;

With[{
  x = Table[Inactive@Unique[], {i, $userInt}]
  }, 
   rules = {_Integer :> x} // Activate
]
{_Integer :> {Unique[], Unique[], Unique[], Unique[], Unique[]}}
{1} /. rules
{{$27, $28, $29, $30, $31}}
share|improve this answer

So many ways:

Apply[Unique, (patt :> #) & @ Table[{}, {3}], {2}]

(patt :> Evaluate@Table[foo[], {3}]) /. foo -> Unique

Hold[Unique[]][[Range[3]^0]] /. _[x__] :> (patt :> {x})

(patt :> {##}) & @@ Table[Unevaluated @ Unevaluated @ Unique[], {3}]

(Function @@ {patt :> Evaluate@Table[#[], {3}]}) @ Unique

All produce:

patt :> {Unique[], Unique[], Unique[]}

But I must say I don't see why you wouldn't just use:

rule = patt :> Table[Unique[], {3}];

patt /. rule
patt /. rule
{$3, $4, $5}

{$6, $7, $8}
share|improve this answer

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.