How are symbols evaluated
Suppose we start with an undefined symbol
We can assign a value to this symbol using = (Set)
How does Mathematica know to replace every instance of x with its value?
It uses a global look-up table. We can find the rules for evaluating symbols by calling OwnValues
The symbol :→ (typed :>) means replace the lhs with the rhs, and only then evaluate the rhs
HoldPattern is a way of expressing a rule for a symbol without having the symbol transformed by other existing rules
Since we used SetDelayed, we see that in this case y is replaced by a new random number each time
How functions are evaluated
Recall our Fibonacci sequence
fib has no OwnValues. That’s because if we evaluate
no rule is applied. Rules are only applied if we give an argument to fib
These kinds of rules (that require arguments) are known as DownValues
The order of these rules shows the order in which Mathematica will attempt to apply them
There are also UpValues and SubValues, but they are a bit obscure...
Understanding function evaluation
Trace helps us understand the order in which Mathematica evaluates expressions
Trace will not show the inner workings of all functions
However, FindRoot supports EvaluationMonitor