1
$\begingroup$

I have the following Function:

Testing[g[x_], y_] := Module[{s = x, t = y, c }, c = s + t ; c] 

Which I am just trying to get to work before doing my actual application. It should accept a function g[x] and do stuff with it.

As a test, I try:

f[x_] := x^2;
Testing[f[x], 4]

But it just returns

Testing[x^2, 4]

which means that it does not understand what I mean...? What is the correct syntax to feed a mathematical function into my Function?

$\endgroup$
3
  • $\begingroup$ Testing will only work for something with head g while f[x] evaluates to Power[x,2]. $\endgroup$
    – Kuba
    Commented Nov 23, 2016 at 16:02
  • $\begingroup$ Why not just run Testing[x_, y_]? $\endgroup$
    – Feyre
    Commented Nov 23, 2016 at 16:09
  • $\begingroup$ If you want it to accept a function in the form g[x], but with any kind of name (instead of g specifically) and any kind of variable, then you should use Testing[g_[x_], y_] := .... Moreover, you should probably make Testing HoldFirst to keep the function from evaluating too early: SetAttributes[Testing, HoldFirst] $\endgroup$ Commented Nov 23, 2016 at 16:34

1 Answer 1

2
$\begingroup$

As I understand you, what you want is that c becomes x^2 + 4. This gives you that polynomial with an anonymous variable instead of x:

Testing[g_, y_] := Module[{s = g[#], t = y, c}, c = Evaluate[s + t] &; c]
f[x_] := x^2
Testing[f, 4]

4 + #1^2 &

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.