Let's walk through the evaluation of flog[1, 2]
, step-by-step...
flog[1, 2]
When this expression is evaluated, Mathematica will substitute 1
for x
and 2
for y
in the definition of flog
given in the question. This yields the next step in our tour:
flog[1, 2] =
f[1, 2] /. v_ :> (PutAppend[Unevaluated[flog[1, 2] = v;], $runningLogFile];
v)
Note carefully that the assignment here, flog[1, 2] = ...
, is part of the definition of flog
itself.
/.
is an infix operator that is an alternate representation of the ReplaceAll function. ReplaceAll
will apply a replacement rule to the value of the first argument. Hold that thought -- we'll come back to it. The first argument is flog[1, 2] = f[1, 2]
. This expression will evaluate f[1, 2]
and then assign the result to flog[1, 2]
. For the sake of discussion, let's assume that f[1, 2]
returns 345
. Thus, a new definition will be added to flog
, namely flog[1, 2] = 345
. After assignment, we can check the definition of flog
:

Observe that where flog
only had a single definition initially but now it has two -- the newly added flog[1, 2]
definition caching the result of that call. This is frequently called "memoization".
flog[1, 2] = 345
may have the side-effect of establishing a new definition for flog
but, like every expression in Mathematica, it yields a value as well. The value is 345
which, after much ado, will be the first argument to ReplaceAll
.
The second argument to ReplaceAll
is an invocation of the :>
operator, an infix expression of the RuleDelayed function. In an effort to keep this post to a manageable size, we'll simply note that the rule evaluates to itself in this context.
So, now we have an expression that involves /.
to evaluate...
345 /. v_ :> (PutAppend[Unevaluated[flog[1, 2] = v;], $runningLogFile]; v
A replacement expression matches its first argument (345
) with the pattern component of the replacement rule (v_
). v_
matches 345
(or anything else for that matter) and gives 345
the name v
for purposes of replacement. ReplaceAll
then substitutes 345
for every occurrence of v
in the right hand side of the rule. The result is the next expression to be evaluated...
(PutAppend[Unevaluated[flog[1, 2] = 345;], $runningLogFile]; 345)
Here we have two expressions separated by a semicolon. Incidentally, ;
is an infix operator that expands to CompoundExpression. The first expression involves PutAppend which writes the value of its first argument to the file named as the value of the second argument. Note, however, that the first argument is wrapped in Unevaluated. This suppresses the evaluation of the first argument so that it will be written exactly as-is to the file: flog[1, 2] = 345;
. Should the current Mathematica session end, the written expression can be read into a future Mathematica session to re-establish the memoized result for flog[1, 2]
.
CompoundExpression
discards the value of all arguments except the last. Here, the last argument is 345
. Since we have come to the end of our expression, this will be the final return value of the original call. That is, flog[1, 2]
returns 345
-- although as we saw there were side-effects that saved this result to memory and disk for future reference.
Future calls to flog[1, 2]
Now if flog[1, 2]
is called again, Mathematica will find the new definition flog[1, 2] = 345
. 345
will be returned directly, without any of the complications that we discussed above. In particular, it won't even call f[1, 2]
again. This, of course, was the whole motivation for this example. The assumption was that f
was very expensive to calculate, justifying all of these gymnastics to minimize the number of times that calculation occurs.