But f[i] is not a list.
Of course not: The problem owner neither uses the word list nor does (s)he use list syntax like f[[i]]
. Possibly an associative array is meant:
In[1]:= Clear[f]
For[i = 1, i < 12, i += 2, f[i] = 9 i]
In[4]:= Table[f[o], {o, 1, 12}]
Out[4]= {9, f[2], 27, f[4], 45, f[6], 63, f[8], 81, f[10], 99, f[12]}
Now problem owners code appears to contain only two incorrect characters: :
and _
. But that does not reproduce problem owners example. If (s)he really wants the i
printed in the results, (s)he could do
In[5]:= Clear[f, i]
For[o = 1, o < 12, o += 2, f[o] = 9 o i]
In[7]:= Table[f[k], {k, 1, 12}]
Out[7]= {9 i, f[2], 27 i, f[4], 45 i, f[6], 63 i, f[8], 81 i, f[10], 99 i, f[12]}
This implicit i
can be considered as bad style, so write
explicitly
In[20]:= Clear[f,x]
For[i = 1, i < 12, i += 2, f[i, x_] = 9 i x]
In[26]:= Clear[i]
Table[f[k, i], {k, 1, 12}]
Out[27]= {9 i, f[2, i], 27 i, f[4, i], 45 i, f[6, i], 63 i, f[8, i], 81 i, f[10, i], 99 i, f[12, i]}
if one does not clear the i
it has value 13
and the result looks strange. Preferable
Clear[f]
f[i_ /; OddQ[i] && 1 <= i <= 11, x_] := 9 i x