I'm confused about the logic behind mathematica programming.

We can have function that will be called doing $f[x]$, thus the element $f[x]$ can be seen as the return value of a function taking an argument $x$ as input.

However, we can also write :

$$ a[1]=2$$

Here, $a[1]$ is not seen as the return value of a function but as a variable named $a[1]$ that has the value $2$.

Now let's take a more tricky example and I do the following :

f[x_] := x^2;





f[1] = 100;





As we can see, I can replace the value $f[1]$ by $100$, but the function still exists in the end.

My questions :

  • What is exactly the quantity $a$ in my example. In another programming language we would call it an array but here it is different.
  • What happens in mathematica when I did the replacement $f[1]=100;$ Because it didn't destroyed the function ($f[2]$ still had a value), but in the same time the function doesn't exist for the value $1$. I don't understand what mathematica does exactly. In a lot of other programming languages, this wouldn't be a valid operation this $f[1]=100;$.

Actually my questions are also to understand the philosophy behind the language.

  • 2
    $\begingroup$ Read about DownValues $\endgroup$ – Alan Dec 29 '18 at 16:44
  • 1
    $\begingroup$ @StarBucK You nicely exemplified how both, functions and variable assignment are embedded in Mathematica's evaluation model. The thing to realise is, that there is no real function evaluation nor variable assignment, there is only teaching new transformation rules to the pattern matcher, which then get triggered, when matching expressions show up. Essentially, there are only symbolic expressions symbol[expr1,expr2,1,"a",...], transformation rules (builtin or added via =(Set) or :=(SetDelayed)), and the pattern matcher with its evaluation model. Everything else follows from there. $\endgroup$ – Thies Heidecke Dec 29 '18 at 19:17
  • $\begingroup$ Also Associating Definitions with Different Symbols is a good place to start understanding Mathematica's expression model better. $\endgroup$ – Thies Heidecke Dec 29 '18 at 19:22

Browse other questions tagged or ask your own question.