Using Integrate, I can do any of the following:

Integrate[x^2, {x,0,2}]

Clear[f]; f[x_]:=x^2; Integrate[f[x],{x,0,2}]

Clear[f]; f=x^2; Integrate[f,{x,0,2}]

and all three return 8/3, as they should. When I asked this question, the accepted answer works, as I just discovered, in the first and second cases, but not in the third. In the third case, using HoldAll prevents the first argument from being properly evaluated. For reference, a simplified version of the solution proposed in the above referenced question, which simply returns the given function with variable name translated to an internal local, is:

i[fn_, intvl_] := 
 Module[{ii, externalSymbol, localVar}, 
  externalSymbol = 
   ReleaseHold[Hold[intvl] /. {x_, y__} :> HoldPattern[x]];
  ii = ReleaseHold[Hold[fn] /. externalSymbol :> localVar];
SetAttributes[i, HoldAll];

When invoked as above (with i in place of Integrate), the three invocations return respectively localvar$<number>^2, localvar$<number>^2, x^2.

Is it possible to define i so that its parameter interpretation behavior matches that of Integrate?

  • 2
    $\begingroup$ But Integrate is not HoldAll. p.s. take a look at table implementation in this answer: mathematica.stackexchange.com/a/567/5478 $\endgroup$ – Kuba Apr 12 '18 at 12:54
  • $\begingroup$ @Kuba That's pretty interesting. So I wonder why the answer to the question I linked to above gave a much more complicated solution? The point of that original question was to isolate the variable name in the parameter list from any values that it held outside of the function. (I know that that wasn't your answer.) $\endgroup$ – rogerl Apr 12 '18 at 15:55

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.