The question contains conflicting requirements. On the one hand, it wants funclist
to behave like a list of functions so that we can write funclist[[2]][4]
and get a value. On the other hand, it wants funclist
to act like a list of expressions in x
so that we can write Plot[funclist[[2]], {x, -1, 5}]
. The two requirements are incompatible: we will need to choose between them.
Functions
If we wish funclist
to behave like a list of functions, we can do this:
parameters = {a, 3, 6, E, 2};
funclist = Function /@ (parameters # ^ Range@Length@parameters)
(* {a #1 &, 3 #1^2 &, 6 #1^3 &, E #1^4 &, 2 #1^5 &} *)
Then:
funclist[[2]][4]
(* 48 *)
But we must pass an explicit argument to one these functions in order to plot it:
Plot[funclist[[2]][x], {x, -1, 5}]
Expressions in x
Alternatively, we can have funclist
contain a list of expressions in x
:
funclist = parameters x ^ Range@Length@parameters
(* {a x, 3 x^2, 6 x^3, E x^4, 2 x^5} *)
But now we cannot invoke them like functions. To evaluate them, we need to replace x
:
funclist[[2]] /. x -> 4
(* 48 *)
Plotting, on the other hand, does not require anything special:
Plot[funclist[[2]], {x, -1, 5}]
Academic Exercise: Both?!
Earlier it was stated that we cannot have funclist
behave both as a list of functions and as a list of expressions. This is not entirely true. If we are willing to damn the torpedoes and steam in anyway, it is possible to make it happen. This is not a serious proposal. It is simply a demonstration that the Wolfram language is very malleable, if we are determined enough.
We start by defining xfuns
, which masquerades as an innocent list. In fact, it serves as a factory to produce these hybrid function/expression objects:
Format[xfuns[e___]] := {e}
xfuns[e___][[i_]] ^:= With[{a = {e}[[i]]}, xfun[a]]
Next we define xfun
, a wrapper that implements the hybrid behaviour:
xfun[e_] /; MatchQ[Stack[], {___, Null, xfun, MatchQ}] := Function @@ {{x}, e}
xfun[x_] := x
This nasty piece of business looks up the call stack to see whether or not it is being applied to an argument. If it is, it returns a function. Otherwise it returns an expression.
Here it all is in action:
funclist = xfuns @@ (parameters x ^ Range@Length@parameters)
(* {a x, 3 x^2, 6 x^3, E x^4, 2 x^5} *)
funclist
looks like a list of expressions in x
. The individual elements resolve to expressions as well:
funclist[[2]]
(* 3 x^2 *)
Plot
agrees:
Plot[funclist[[2]], {x, -1, 5}]
But... they can be used as functions too!
funclist[[2]][4]
(* 48 *)
Pure evil. I emphasize again: this is not a recommended practice. This is just for fun. Programming in this style means that expressions evaluate in unexpected ways, creating a fertile breeding ground in which bugs can thrive.