I asked a similar question there: Define function of a list from a function of its elements (SetDelayed).
This one is different because I'd like to use Set
instead of SetDelayed
, and it makes a big difference.
Let's say I have a long expression which depends on x[1]
, x[2]
, ..., for example here simply 3x[1]+x[5]
. I want to define a function based on this expression, but of course I don't want to rewrite it manually as:
f[{x1_,x2_,x3_,x4_,x5_}] = 3x1 + x5
Just to explain better, I'd looking for something like the incorrect
expr = 3x[1] + x[5]
f[Array[x_]] = expr
so that f[{a,b,c,d,e}]
would return 3a + e
.