How can symbols from assignments, ie left hand side in expressions Set and SetDelayed be matched after importing using NotebookImport?

Consider a minimal notebook with these mixed definitions:

selPos = Select[#>0&];
selNeg[x_]:= Select[x<0];
selNegPair[x_, y_] := Select[x < 0 && y < 0];

There may be more involved definitions with SubValues, UpValues etc but these are the main cases to solve for. Note the typical use of CompoundExpression.

After importing via NotebookImport using the "Input" option:

exampleInput = NotebookImport["Example.nb","Input"]


Note the assignments are wrapped in HoldComplete (rather than HoldPattern output by OwnValues - is there an option for NotebookImport that will preserve the OwnValues?)

Is there an easier way to extract these symbols than to use 2 replaces and release hold? first to match the left hand of Set or SetDelayed, and second to extract the symbol from the HoldComplete, it seems to work only if HoldForm is used in the replacement rule, and finally hold is released:

exampleInput /. {HoldPattern[Set[f_,_]]:> f,HoldPattern[SetDelayed[f_,_]]:> f}/. {HoldComplete[g_[___];]:> HoldForm[g],HoldComplete[g_;]:> HoldForm[g]} // Map[ReleaseHold]

Gives the desired symbols - but only when evaluated in a separate kernel, otherwise releasing the hold will replace the symbols by their values:


Is there an easier way to accomplish the above?

  • 2
    $\begingroup$ What exactly is what you want to do? Do you want to print a list of symbols names or to create an expression/container with those symbols kept unevaluated? $\endgroup$
    – Kuba
    May 20, 2020 at 7:23
  • $\begingroup$ The goal is to build an index or table of which functions/symbols defined in the imported notebook are used in other notebooks - in this case the latter are chapters of a book. The lookups can be done by converting the symbols ToString and importing the target notebooks using NotebookImport[...,"Input"->"InputText"] $\endgroup$ May 20, 2020 at 17:44
  • $\begingroup$ @Kuba, cont from above, a related application is to take the subset of definitions that appear at least in one of the target notebooks (some definitions might not be used in the project) and automatically generate a new notebook with the definitions sorted by the by some criterion, eg alphabetically by symbol or first chapter in which they appear. Should I ask as a separate Q or edit the above? $\endgroup$ May 20, 2020 at 18:08

1 Answer 1

, HoldPattern[(Set | SetDelayed)[f_Symbol | f_[___], _]] :> Hold[f]
, {2, 3} 
] // Apply[Join]
Hold[selPos, selNeg, selNegPair]

Does it fit your needs?

{2,3} is there to handle HoldComplete[ _ ] and HoldComplete[ _ ; ], but I am not sure how flexible it should be because the input cell can contain {{{{{ f =1 }}}}} for example. Then ReplaceAll is a must I guess.

  • $\begingroup$ Thanks, this works except for UpValue definitions. I only added the pattern f_[___][___] to match definitions with SubValues. Assuming that the imported notebook has to be evaluated for this method to be useful, Is it possible to release Hold in the same kernel as without evaluating the definitions? $\endgroup$ May 20, 2020 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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