Function in Table

Function has the attribute HoldAll, so the reference to i in the Table expression will not be expanded.

However, you can use With to inject the value into the held expressions:

Table[With[{i = i}, a[[i]]*Sin[#] &], {i, 3}]
{a[[1]] Sin[#1] &, a[[2]] Sin[#1] &, a[[3]] Sin[#1] &}

This issue will be present not only for Function but for all expressions that hold their arguments (via attributes like HoldFirst) -- for example: Plot, Dynamic, RuleDelayed (:>) etc.

The solution using With is mentioned in the tutorial "Introduction To Dynamic / A Good Trick to Know".


. . . & is a held expression. (Function has attribute HoldAll.)

Injector pattern to the rescue:

Range@3 /. i_Integer :> (a[[i]] Sin[#] &)

Replace[Range@3, i_ :> (a[[i]] Sin[#] &), 1]

Table[j /. i_ :> (a[[i]] Sin[#] &), {j, 3}]

Or using \[Function] and Array:

Array[i \[Function] (a[[i]] Sin[#] &), 3]

In this case you could do the replacement the other direction but you will need to hold i to protect it from a global value:

Table[a[[i]] Sin[#] & /. HoldPattern[i] -> j, {j, 3}]

Or use Block:

Block[{i},
  Table[a[[i]] Sin[#] & /. i -> j, {j, 3}]
]

This works, but only because j is undefined:

Table[(a[[j]]*Sin[#] &) /. j -> i, {i, 3}]

(if we do j = 5; Table[(a[[j]]*Sin[#] &) /. j -> i, {i, 3}] then it fails; one could localize this with Module to get it to work anyway).

Or, if you hate brevity and compactness:

cF = Function[{j}, a[[j]]*Sin[#] &];
Table[
 cF[j],
 {j, 1, 3}
 ]

Personally I'd use either this last form or WReach's/Rojo's way.