Create rule whose LHS is already defined
I suggest wrapping the LHS a[t]
in the Rule
you are trying to return in HoldPattern
:
Clear[func]
func[k_] := Block[{a},
a[t_] := t^k;
{HoldPattern[a[t]] -> a[t]}
]
func[2]
(* Out: {HoldPattern[a[t]] -> t^2} *)
a[t] /. func[5]
(* Out: t^5 *)
func1[k_] := ReleaseHold @ Block[{a}, a[t_] := t^k; {Hold[a[t]] -> a[t]}]
func2[k_] := Activate @ Block[{a}, a[t_] := t^k; {Inactive[a[t]] -> a[t]}]
func1[3]
{a[t] -> t^3}
func2[3]
{a[t] -> t^3}
a[t] /. func1[5]
t^5
a[t] /. func2[x]
t^x
Could return pure functions instead:
pow[k_] := a -> Function[t, t^k]
Examples:
a[t] /. pow[5]
a[10] /. pow[x]
a[2] /. pow[3]
t^5
10^x
8