0
$\begingroup$

I have the a list A and I want to define a function f that takes out the $i$-th element from the list A:

A = {5, 2, 30, 14}

f[1] = 5;
f[2] = 2;
f[3] = 30;

I've tried f[i_] = A[[i]] but it didn't work.

$\endgroup$
  • 2
    $\begingroup$ But why do this when you can just do A[[2]]? $\endgroup$ – march Jul 6 '19 at 15:52
  • $\begingroup$ You are right, it is not necessary to define a function for this. $\endgroup$ – Igor Jul 7 '19 at 15:32
3
$\begingroup$
f[i_] := A[[i]]

I think you would profit from learning the distinction between immediate and delayed assignments.

|improve this answer|||||
$\endgroup$
  • $\begingroup$ Thanks Roman!! I am new at Mathematica. $\endgroup$ – Igor Jul 6 '19 at 15:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.