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

1 Answer 1

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$
1
  • $\begingroup$ Thanks. Interestingly, it seems the following does not work: Evaluate[#] &[2] $\endgroup$ Commented Dec 4, 2015 at 10:34

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.