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$

closed as off-topic by march, m_goldberg, rhermans, garej, Henrik Schumacher Jul 8 at 17:24

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – march, m_goldberg, rhermans, garej, Henrik Schumacher
If this question can be reworded to fit the rules in the help center, please edit the question.

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

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

$\endgroup$
  • $\begingroup$ Thanks Roman!! I am new at Mathematica. $\endgroup$ – Igor Jul 6 at 15:20

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