# Trying to calculate a sum within a module - why is the iterator not creating integers?

Suppose I have the code:

#!/usr/local/bin/MathematicaScript -script
SetOptions[$Output, FormatType -> OutputForm]; foo[bar_?IntegerQ, baz_?IntegerQ] = bar; M[g_?IntegerQ] = Module[ {sum}, sum = Sum[foo[i, g], {i, 1, 4} ]; N[sum] ]; Print[M[1]]  Why does this output: foo[1., 1] + foo[2., 1] + foo[3., 1] + foo[4., 1]  When simply changing the g to a 1 as following correctly interprets the iterator as an integer? #!/usr/local/bin/MathematicaScript -script SetOptions[$Output, FormatType -> OutputForm];

foo[bar_?IntegerQ, baz_?IntegerQ] = bar;

M[g_?IntegerQ] = Module[
{sum},
sum = Sum[foo[i, 1], {i, 1, 4} ];
N[sum]
];

Print[M[1]]


This outputs (as expected):

10

-
I've added an answer below, but I'm still trying to figure out what exactly your function is for. The way it is written, all it does is sum the range from 1 to 4 no matter what argument is passed. – kale Sep 8 '12 at 14:36
Thanks! Don't worry about what this is for. I created this smaller instance quickly from an instance in a larger script, which is why the N[sum] part is in it. I don't know how I overlooked the SetDelayed option; I've been looking at it for too long... Thanks! – Brandon Amos Sep 8 '12 at 14:45
10-4. No problem. – kale Sep 8 '12 at 14:46

You get your desired result by invoking the SetDelayed option for your functions. To output an Integer, remove the N[] function.

#!/usr/local/bin/MathematicaScript -script
SetOptions[\$Output, FormatType -> OutputForm];

foo[bar_?IntegerQ, baz_?IntegerQ] := bar;

M[g_?IntegerQ] := Sum[foo[i, g], {i, 1, 4}];

Print[M[1]]

(*10*)

-