# Pattern Match and Replace Expression involving Pure Function

Let's say I have an expression that in held form contains a pointer to a function.

expr = Hold[2*{"FUNCTION1"} + 5];


{"FUNCTION1"} references a pure function or combination of other functions that are verbose and would be redundant to retype. For an example, let's say the function is:

func = #^2 &


In later calculations, I want to define a new function that uses expr with the referenced function substituted:

newfunc = ReleaseHold[expr /. {"FUNCTION1"} :> func]


5 + 2 (#1^2 &)

However, this is not what I'm after as trying to evaluate newfunc reveals:

newfunc


(5 + 2 (#1^2 &))

What I'm really after is

(5 + 2*#1^2) &


55

So, is there a pattern format that can be used to correctly splice in the pure function. This needs to be general as expr won't always have the same form.

• Related: (28064) – Mr.Wizard Dec 29 '14 at 23:23
• Thanks for the Accept. I am curious to know how (where) you are using this method. – Mr.Wizard Dec 31 '14 at 1:42
• @MrWizard I'd be glad to show you, but it'll require something more substantial than a comment box to explain. I still have your email if you're that interested. – kale Dec 31 '14 at 2:11
• Go ahead; thanks. – Mr.Wizard Dec 31 '14 at 5:04
• @Mr.Wizard Email sent. – kale Jan 5 '15 at 16:55

## 2 Answers

I seem to recall a similar question but I cannot find it now. Anyway a key detail is that the body of the function presumably should not be evaluated, meaning e.g. func = Print[#^2] & should still work.

Here is one approach:

expr = Hold[2*{"FUNCTION1"} + 5];

func = Print[#^2] &;

newfunc = Function @@ expr /. {"FUNCTION1"} -> Hold @@ func // ReleaseHold

2 Print[#1^2] + 5 &


Maybe these other approaches will be useful for you pointer to function problem :

## 1.expr[x_] := 2*func[x] + 5

You can now for example always use expr but switch func to your needs. For example :

func = Sin ; expr[hello]
func = #^2 &; {expr[hello], expr}


return

5 + 2 Sin[hello]
{5 + 2 hello^2, 55}

But if you really need to create explicitly a new function, you can do :

func = #^3 &;
expr2[x_] = expr[x]


5 + 2 x^3

(*check*)
expr2
(* 7 *)


or if you prefer the Function approach :

func = Tan;
expr3 = Function[x, Evaluate@expr@x]


Function[x, 5 + 2 Tan[x]]

(*check*)
expr3
(*5 + 2 Tan*)


## 2. fexpr[func_][x_] := 2*func[x] + 5

This approach is even more useful I guess.

Then for example :

fexpr[Sin]


5 + 2 Sin

fexpr[#^2 &]


55

Plot[fexpr[#^2 &][x], {x, 0, 1}] If you need to create new functions, then for example :

f1 = fexpr[Cos][#] &


fexpr[Cos][#1] &

f1[Pi/3]


6

In this previous case you see that f1 is defined as a function of fexpr. This way, you can track what was your input function. But maybe this is exactly what you don't want, then you can do instead :

f2 = Function[x, Evaluate@fexpr[Tan][x]]


Function[x, 5 + 2 Tan[x]]

Here f2 is not a function of fexpr anymore (no more traces how the function was created).

(*check*)
f2
(*5 + 2 Tan*)


I haven't check, but there is probably here a lot of other posts and answers for this kind of problems.

• I don't know if this is applicable to the OP's "template" code but it is good information so +1. Releated: (7999) – Mr.Wizard Dec 31 '14 at 1:44