0
$\begingroup$

This question is basic, but I do not know what is wrong.

Clear[a0,a]
a0=n;
a[n_]:=a0

But when I evaluate a[n-1] the output gives n which is same as a[n], it should be n-1 but why is it n? Could someone spot the error?

$\endgroup$
  • 3
    $\begingroup$ Can't you just do a[n_] := n?? The reason this happens is that there is nothing that matches the pattern n_ on the right hand side of the definition og a[n_]. So no matter what you give as input, it will match n_, but then this won't be used anywhere, Mathematica will just return a0, which contains the symbol n, not the thing that matched the patter n_. $\endgroup$ – Marius Ladegård Meyer Jun 14 '16 at 15:31
  • 2
    $\begingroup$ Possible duplicates?: (11461), (69590) $\endgroup$ – Mr.Wizard Jun 14 '16 at 15:46
  • $\begingroup$ @MariusLadegårdMeyer, I can not do that. This is just a simple example of my problem bigger code which run that way. But thanks for trying to help, I appreciate it. Thanks :) $\endgroup$ – Chen M Ling Jun 15 '16 at 3:33
  • $\begingroup$ @Mr.Wizard Hi Thanks for giving the links. I tried one of the suggestion given in the link and it works. Thanks Mr :) $\endgroup$ – Chen M Ling Jun 15 '16 at 3:34
  • 1
    $\begingroup$ I am glad your problem was solved. If in the future a question of yours is closed as a duplicate but you find that solutions given do not work, or your problem was actually different, please know that you can edit your question to explain this and it will be added to the Review Queue for reopening. $\endgroup$ – Mr.Wizard Jun 15 '16 at 6:39

Browse other questions tagged or ask your own question.