11

Say, I have some function f[a_,b_,c_]=a+b+c. Now I need to define another function,g[f_,d_]=f+d, where the f here should be replaced by the definition of f, resulting in a+b+c+d.

How do I do this? I tried g[f_,d_]=f+d/.f->Definition[f], however that didn't work.

1
  • 3
    Your question seems to be: "How do I define a function that takes another function as one of its arguments?" Commented Apr 4, 2011 at 23:37

5 Answers 5

15

Well, your setup requires that g be a function of a, b, and c, since you explicitly made f a function thereof. Could instead do:

f[a_,b_,c_]=a+b+c;

g[f_,a_,b_,c_,d_] = f[a,b,c]+d

Out[2]= a + b + c + d

Daniel Lichtblau

Sign up to request clarification or add additional context in comments.

Comments

3

Just an aside note.

The whole thing makes more sense when both functions share the same arguments, or the arguments for the second function are implicit in the first.

For example:

f[x_] := 1 + 1/x;

g[h_, x_] := h[x]^x;

Limit[g[f, x], x -> +Infinity]  
-> E

Or something like

f[x_] := EuclideanDistance[x, #] &

g[f_, x_] := If[f[2 x]@x < 3, x, 2 x]

g[f, 3]
->6

g[f,2]
->2

Comments

2

Daniel's answer is the right one - it answers what your question should have asked!

But just for fun here's something closer to what your question actually asked.

f[a_,b_,c_] := a + b + c

g[f_, d_] := Catch[Module[{dv},
  Check[dv = DownValues[f], Throw[$Failed], {DownValues::sym}];
  Switch[Length[dv],
         0, Print["No DownValues"]; $Failed,
         1, dv[[1, 2]] + d,
         _?(# > 1 &), Print["More than one DownValue - using the first!"]; 
                      dv[[1, 2]] + d,
         _, Print["huh?!"]; $Failed]]]

test it:

In[3]:= g[f, d]
Out[3]= a + b + c + d

Define another function:

ff[a_, b_, c_] := a b c

In[5]:= g[ff, d]
Out[5]= a b c + d

and give it a second definition:

In[6]:= ff[a_, b_] := a + b
In[7]:= DownValues[ff]
Out[7]= {HoldPattern[ff[a_,b_,c_]] :> a b c, HoldPattern[ff[a_,b_]] :> a+b}

In[8]:= g[ff, d]
During evaluation of In[8]:= More than one DownValue - using the first!
Out[8]= a b c + d

Of course, the fact that you don't pass any arguements to f in g[f_,d_] will (in most cases) make this type of thing pretty pointless...

2 Comments

btw: In the above, "first" does not mean the first definition given. It means the first in the order that Mathematica stores them in.
LOL! I was writing a comment on Daniel's answer explaining why the question makes no sense ... went to a short meeting and when I came back found your answer! You can answer wrong questions too! Pure genius! +1
2

In the definiton

 f[a_, b_, c_] := a+b+c

look at a, b and c as the first, second and third argument at the function call and NOT symbols a,b or c. Similar as in f := (#1 + #2 + #3)&, the call to f itself (without arguments) will not give desired results.

Perhaps you wanted to do something like this:

f := a+b+c

Now, the f is associated as a sum of a, b and c, which are global symbols and not function arguments. Then,

f+d

will give a + b + c + d and you can use ReplaceAll (or /.) for substitution:

f + d /. a->2x

gives b+c+d+2x. 2 f will give 2 (a+b+c), Sin[f] will give Sin[a+b+c], f-c will give a+b etc...

Comments

2

I also I find it difficult to discern your intent, but this is my interpretation:

f[a_, b_, c_] := a + b + c
g[f_, d_] := f[##] + d &;

g[f, 3]

  (* Out=  f[##1] + 3 &     *)

%[q, r, s]

  (* Out=  3 + q + r + s    *)

g[f, 5][1, 2, 3]

  (* Out=  11    *)

This way, g[f, x] returns a function which passes its arguments to f.

Comments

Your Answer

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