0
$\begingroup$

If I want to get the solution for a simple function, e.g. a^x, I can use:

a = 2;
Function[x, a^x][2]

However, if I have the function in a variable, I can't get it to work:

a = 2;
func = a^x
Function[x, func][2]

Similarly, this doesn't work as wished, which is that the last line provides the result if 2 is put into x:

a = 2;
func = a^x;
der = D[func, x]
Function[x, der][2]
$\endgroup$

marked as duplicate by Kuba, m_goldberg, dr.blochwave, user9660, Bob Hanlon Dec 4 '15 at 14:21

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ Function is HoldAll so Function[x, #][2] &[func], let me find a duplicate. $\endgroup$ – Kuba Dec 4 '15 at 10:10
  • 2
    $\begingroup$ duplicates?: 14686, 22033 related: 10067, 40445, 31985 $\endgroup$ – Kuba Dec 4 '15 at 10:17
  • $\begingroup$ Intersting. But this doesn't work: # &[2] &[func] $\endgroup$ – Mockup Dungeon Dec 4 '15 at 10:30
  • $\begingroup$ Because it isn't what I've suggested ;) $\endgroup$ – Kuba Dec 4 '15 at 10:31
  • $\begingroup$ @Kuba Sure! You didn't suggest it, I was just wondering whether this works as well. $\endgroup$ – Mockup Dungeon Dec 4 '15 at 11:38
1
$\begingroup$

Using Evaluate,

a = 2;
func = a^x;
Function[x, func][2]
Function[x, Evaluate@func][2]
(* 2^x *)
(* 4 *)

and

a = 2;
func = a^x;
der = D[func, x];
Function[x, der][2]
Function[x, Evaluate@der][2]
(* 2^x Log[2] *)
(* 4 Log[2] *)
$\endgroup$
  • $\begingroup$ Thanks. Interestingly, it seems the following does not work: Evaluate[#] &[2] $\endgroup$ – Mockup Dungeon Dec 4 '15 at 10:34

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