Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

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'm trying to write a custom function but it isn't returning any output.

Here's my code so far:

Options[Alternate] = {AlternationType -> AlternationPlusMinus};

Alternate[Terms_, Exponent_, OptionsPattern[]] := (
   tmpSet = {};
   If[OptionValue[AlternationType] == AlternationPlusMinus, 
Do[If[Mod[counter, 2] != 0, 
  AppendTo[tmpSet, (counter^Exponent) - 1] AppendTo[
    tmpSet, (counter^Exponent) + 1]], {counter, Terms}], 
Do[If[Mod[counter, 2] != 0, 
  AppendTo[tmpSet, (counter^Exponent) + 1] AppendTo[
    tmpSet, (counter^Exponent) - 1]], {counter, Terms}]];
   Return[Expand[InterpolatingPolynomial[tmpSet, x]]];
Alternate[20, 2]

When I execute it, no output block is even generated, let alone any error messages or warnings, is there any way to fix it?


share|improve this question
When I execute it in a fresh Mathematica kernel, I do get some output (a very large polynomial) and some error messages like (Thread::tdlen: Objects of unequal length in {0} {0,2} cannot be combined. >>). Have you tried this in a fresh kernel? Also, you are not localizing tmpSet. Try wrapping your function with Module. Also, try Fold instead of Do and Return. – Verbeia Jul 1 '12 at 21:55
up vote 4 down vote accepted

Chris's answer (particularly the missing commas) explains why your version wasn't working. However you should also consider modularising your code so that tmpSet doesn't leak out to have a value elsewhere in your code:

Alternate[Terms_, exponent_, OptionsPattern[]] := Module[{tmpSet = {}},
  If[OptionValue[AlternationType] == "AlternationPlusMinus", 
   Do[If[Mod[counter, 2] != 0, 
     AppendTo[tmpSet, (counter^exponent) - 1], 
     AppendTo[tmpSet, (counter^exponent) + 1]], {counter, Terms}], 
   Do[If[Mod[counter, 2] != 0, 
     AppendTo[tmpSet, (counter^exponent) + 1], 
     AppendTo[tmpSet, (counter^exponent) - 1]], {counter, Terms}]];
  Return[Expand[InterpolatingPolynomial[tmpSet, x]]]]

Also, the whole Do...Return paradigm is almost never the most efficient way to program in Mathematica. Here is a more functional-programming style alternative. It gives the same answers for the simple tests I have done. Notice I have restricted the definition of the function to only match for positive integer values of terms and exponent. You can of course remove them again if you intend for this function to be used for real or negative-valued inputs.

alternateAlternate[terms_Integer?Positive, exponent_Integer?Positive, OptionsPattern[]] := 
 Expand[InterpolatingPolynomial[#, x]] &@
  If[OptionValue[AlternationType] == "AlternationPlusMinus", 
   FoldList[(#2^exponent) + If[OddQ[#2], -1, 1] &, 0, Range[2, terms]], 
   FoldList[(#2^exponent) + If[OddQ[#2], 1, -1] &, 2, Range[2, terms]] ]
share|improve this answer
Thank you so much! I've used the second solution combined with Chris' quotes around AlternationPlusMinus. It works perfectly now! :) – Callum Booth Jul 2 '12 at 15:48

You could put AlternationPlusMinus in quotes. You also have two missing commas in the If statements. Exponent is a reserved word but doesn't prevent execution. This version seems to work:

Options[Alternate] = {AlternationType -> "AlternationPlusMinus"};

Alternate[Terms_, exponent_, OptionsPattern[]] := (tmpSet = {};
   If[OptionValue[AlternationType] == "AlternationPlusMinus", 
    Do[If[Mod[counter, 2] != 0, 
      AppendTo[tmpSet, (counter^exponent) - 1] , 
      AppendTo[tmpSet, (counter^exponent) + 1]], {counter, Terms}], 
    Do[If[Mod[counter, 2] != 0, 
      AppendTo[tmpSet, (counter^exponent) + 1] , 
      AppendTo[tmpSet, (counter^exponent) - 1]], {counter, Terms}]];
   Return[Expand[InterpolatingPolynomial[tmpSet, x]]];);
Alternate[20, 2]
share|improve this answer
This still gives the Thread::tdlen error for me, and I do get an output. I'm currently trying to redo this function using FoldList but getting different answers. – Verbeia Jul 1 '12 at 22:08
@ Verbeia - I have added to my answer. Added commas. – Chris Degnen Jul 1 '12 at 22:14
Yes the missing commas was the issue I came up with too. – Verbeia Jul 1 '12 at 22:25

Your Answer


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.