个人认为该说法欠妥
单就title提到的问题, 并不是显式存在的问题, 应该是SetDelayed执行机制问题
我们先看一下Set功能Possible Issues第一条描述:
In the presence of global variables, pattern variables may show unexpected behavior
继续看tutorial/ImmediateAndDelayedDefinitions
开宗明义说了:
You may have noticed that there are two different ways to make assignments in the Wolfram Language: lhs=rhs and lhs:=rhs. The basic difference between these forms is when the expression rhs is evaluated. lhs=rhs is an immediate assignment, in which rhs is evaluated at the time when the assignment is made. lhs:=rhs, on the other hand, is a delayed assignment, in which rhs is not evaluated when the assignment is made, but is instead evaluated each time the value of lhs is requested.
Set是定义时计算, SetDelayed是使用时计算
那么我们来看看这两个的本质区别
就拿title问题举例
b = a;
f[a_] := b
f[2]
这个结果是 a
d = c;
g[c_] = d
g[2]
这个结果是 2
没错, 这对新人来说特别具有迷惑性
但是如果你弄懂了上面的本质区别, 就会知道
g[c_]=d => g[c_]=c
??g 也会看到相同的结论
因此在执行g[2]时, 相当于执行了With[{c=2}, c], 注意, 这里的c是临时变量, 并不是对全局变量c进行赋值
同样的, 执行 f[2], 相当于执行了 With[{a=2}, b]
这里的a依然是临时变量a, 并不是b等于的那个a, 所以b的值依然是a, 不是2
综上所述, 理解这个问题最根本的是明白Mathematica里面Pattern的变量不是实际的变量, 而是占位符的名字罢了
如果系统接受过编程训练, 那就是实参和形参的区别, 至于是Pass By Value还是Pass By Reference, 甚至更细分的 Pass By Reference Call By Value那就不是本帖讨论的范畴了, 具体可以参考维基百科,
https://en.wikipedia.org/wiki/Pass_by_reference