3

I would like to use a Switch command with IntegerPartitions[n] of terms with n ranging from 3 to 12 and n chosen at run time. I have:

monodromyTypes = IntegerPartitions[n];
switchCodes = Flatten[Table[{monodromyTypes[], i}, {i, 1,Length[monodromyTypes]}], 1];

Then:

switchCodes={{4}, 1, {3, 1}, 2, {2, 2}, 3, {2, 1, 1}, 4, {1, 1, 1, 1}, 5}

and I would like now to use switchCodes in a Switch statement like:

Switch[monodromy,{4}, 1, {3, 1}, 2, {2, 2}, 3, {2, 1, 1}, 4, {1, 1, 1, 1}, 5]

However I can't simply code Switch[monodromy,switchCodes]. I suspect I need to use Sequence but do not know how and was wondering if someone could help me.

Thanks.

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Dominic
  • 31
  • 1
  • 2
    Like Switch[monodromy, ##] & @@ switchCodes, then? – J. M.'s missing motivation Mar 19 '17 at 11:44
  • 2
    The reason that you had difficulty using Sequence is that Switch has the attribute HoldRest. Consequently, you would need to use Evaluate to override the HoldRest, i.e., Switch[monodromy, Evaluate[Sequence @@ switchCodes]]. Also, in your initial definition of switchCodes, I believe that monodromyTypes[] should read monodromyTypes[[i]] – Bob Hanlon Mar 19 '17 at 14:39
  • I think this should be marked as a duplicate of (19277), but that Q&A should also be extended to expressly address held arguments even though user5844's answer implicitly solves that. – Mr.Wizard Mar 23 '17 at 04:44

0 Answers0