Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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
share|improve this question
    
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! –  highphi Sep 8 '12 at 14:45
    
10-4. No problem. –  kale Sep 8 '12 at 14:46

1 Answer 1

up vote 3 down vote accepted

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*)
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.