1
$\begingroup$

My question is clarification for the question here Define function using variable list

With doing this

variables = {a, b};
f = Function[Evaluate@variables, 2 a + b];

I can define a function of the array of variables. Now, I would like to define a function f2 which is a function of the previous one. Just writing

f2 = Function[Evaluate@variables, f1@@variables -2*a];

doesn't work. What is correct syntax for defining f2? I want to call it like f2[1,2] (and get 2 as an answer, obviously).

$\endgroup$
  • $\begingroup$ What is f1 supposed to be? $\endgroup$ – J. M. is in limbo Mar 28 '19 at 16:53
3
$\begingroup$

Just add another Evaluate in the function body:

variables = {a, b};
f1 = Function[Evaluate@variables, 2 a + b];
f2 = Function[Evaluate@variables, Evaluate[f1 @@ variables - 2*a]];
$\endgroup$
2
$\begingroup$

How about defining things directly:

f[{a_, b_}] := 2 a + b;
f2[{a_, b_}] := f[{a, b}] - 2 a;

then

f2[{1, 2}]
2

and

variables = {1, 2};
f2[variables]

gives the same answer.

$\endgroup$
1
$\begingroup$

Applying the method I recommended in my answer to the Question you referenced:

variables = {a, b};

f1 = Function[Evaluate@variables, 2 a + b];
f2 = variables /. _[vars__] :> Function[{vars}, f1[vars] - 2*a];

f2[1, 2]   (* Out[]= 2 *)

As in that answer I would encourage the use of variables = Hold[a, b]; for robustness, but I want to show that the /. _[vars__] :> form also works on a raw List in case that is a concern.

With the method above the created definition of f2 is Function[{a, b}, f1[a, b] - 2 a]. If instead you want full evaluation of the function body, and you don't need variable holding, you can do this instead:

Function @@ {variables, f1 @@ variables - 2*a}
Function[{a, b}, b]

Or:

Function @@ {#, f1 @@ # - 2*a} &[variables]
Function[{a, b}, b]
$\endgroup$
1
$\begingroup$

I'd like to use this question to promote the ResourceFunction ExpressionToFunction I submitted recently, which is specifically designed to facilitate the creation of Functions from all sorts of expressions and to allow you to call them in a variety of ways.

$\endgroup$
0
$\begingroup$

This works:

variables = {a, b}; 
f1 = Function[Evaluate@variables, 2 a + b]; 
f2 = With[{f1 = f1, v = variables}, 
           Function[Evaluate@v, (f1 @@ v) - 2*a]]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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