The documentation for $Pre gives the following example

SetAttributes[saveinputs, HoldAll];
inputs = {};
saveinputs[new_] := (inputs = {inputs, HoldForm[new]}; new);
$Pre = saveinputs;

Evaluating the 3 input cells a then b then Flatten[inputs] gives


which is the the expected output. For some reason the intermediate step $Pre = saveinputs is necessary, but why? If one tries to define $Pre directly via

SetAttributes[$Pre, HoldAll];
inputs = {};
$Pre[new_] := (inputs = {inputs, HoldForm[new]}; new);

then $Pre does not work and inputs = {}. How is this different?


1 Answer 1


We can only guess. My guess is that it is only applied when it has OwnValues which your second example does not have.

p.s. you can do it in a one run:

inputs = {};
$Pre = Function[new, inputs = {inputs, HoldForm[new]}; new, HoldAll]
  • $\begingroup$ Thank you. I see now that the assignment $Pre = saveinputs is not being handled any differently from, say, f = saveinputs (in terms of OwnValues and DownValues of $Pre and f). It is also nice to see that it can be done with Function ... difficult to use HoldAll with & :) $\endgroup$ Jul 19, 2019 at 9:32
  • 1
    $\begingroup$ @AndrewNorton ... but possible :) mathematica.stackexchange.com/questions/29168/… $\endgroup$ Jul 19, 2019 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.