2
$\begingroup$

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.

$\endgroup$
  • 1
    $\begingroup$ Like Switch[monodromy, ##] & @@ switchCodes, then? $\endgroup$ – J. M. will be back soon Mar 19 '17 at 11:44
  • 1
    $\begingroup$ 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]] $\endgroup$ – Bob Hanlon Mar 19 '17 at 14:39
  • $\begingroup$ 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. $\endgroup$ – Mr.Wizard Mar 23 '17 at 4:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.