I am creating a function fun
which takes a parameter p
and returns a pure function:
ClearAll[fun];
fun[p_] :=
Module[{f, x0},
f = 1 + #^2 &;
x0 = p;
f[#]/f[x0] &]
But the resulting fun
seems to preserve some scoping stuff; for example
fun[0]
(* f$317[#1] / f$317[x0$317] & *)
instead of returning 1 + #1^2 &
as expected. On the other hand, passing an argument to the pure function makes it work normally:
fun[0][x]
fun[1][x]
(* 1 + x^2 *)
(* (1 + x^2) / 2 *)
How can I make fun[0]
return 1 + #1^2 &
?
(The reason I use Module[{f,x0},
...]
is because I want to obtain x0
from solving an equation involving f'
and p
, but that is unrelated to the scoping problem, which occurs even for the simple assignment x0 = p
.)
fun[p_] := Module[{f, x0}, f = 1 + #^2 &; x0 = p; Evaluate[ f[#]/f[x0] ] &]
$\endgroup$