4
$\begingroup$

I have defined a bunch of functions over several different files, and I would like a list of prototypes of all the functions I have defined in my current session (I want the mathematica equivalent of the .h file for my .c file).

For example, if I defined two functions,

f[arg1_, arg2_]:= Module[...];
g[t_]:= Module[...];

Then I would want the following list (alphabetized, of course):

f[arg1_, arg2_]
g[t_]

It would be nice if the list could be exported to a notebook or pdf for quick viewing. How can I accomplish this task?

$\endgroup$
4
  • $\begingroup$ You are aware that referring to this as "the mathematica equivalent of the .h file for my .c file" is actually a misnomer? Mathematica doesn't need header files. $\endgroup$ Nov 19, 2013 at 16:04
  • $\begingroup$ @SjoerdC.deVries C does't need header files either... I could just put prototypes at the top of the .c file. I write header files to give me a clean organized list of all the things that are going to appear in the .c file. It is a coded form of documentation. $\endgroup$
    – nullUser
    Nov 19, 2013 at 19:31
  • $\begingroup$ There are conditions where those headers are needed, for instance in functions that mutually reference each other. Not so in Mathematica. $\endgroup$ Nov 19, 2013 at 21:47
  • $\begingroup$ @SjoerdC.deVries Prototypes are needed for mutually referencing functions. A separate header file is not. $\endgroup$
    – nullUser
    Nov 20, 2013 at 5:10

3 Answers 3

5
$\begingroup$

Here's one solution (that needs a bit of improvement to make it useful):

functionList[context_String] := 
 TableForm@Sort[
   HoldForm @@@ Flatten[
      (ToExpression[#, InputForm, DownValues] & /@ Names[context <> "*"])[[All, All, 1]]]]

Now just do functionList["Global`"].

It does not handle UpValues, SubValues, OwnValues, but most of your functions are likely to use DownValues.


Considering your use case, take a look here too:

Writing usage messages is going to be a much better solution. Simply taking the pattern you used in the function definition (as I did above) might sound like a good idea at first, but it conflicts with some common Mathematica patterns such as memoization.

$\endgroup$
2
  • $\begingroup$ This is perfect for my needs! $\endgroup$
    – nullUser
    Nov 19, 2013 at 20:31
  • $\begingroup$ Hi, Szabolcs. Thank you for your solution. I would like to know further how to list function within current notebook? $\endgroup$
    – matheorem
    Sep 9, 2017 at 6:50
4
$\begingroup$

There is something close to what you ask for built into Mathematica. It is the function called Information. In the form you would use to get what you asked about, there is a keyboard shortcut, the question-mark character.

a = 42;
f[arg1_, arg2_] := Module[{}, {arg1, arg2}]
g[t_] := t^2
?"Global`*"

glob1.png

Each of the symbols shown above is actually a button and gives further information about the definition of the symbol when clicked on. For example, were I to click on f, I'd get

f[arg1_, arg2_] := Module[{}, {arg1, arg2}]

The string "Global`*" is interpreted by Information as meaning "show me all the symbols defined in the Global context". A context is a Mathematica namespace and Global is the namespace where user defined symbols go unless you do something special to prevent it.

Note that this works only for symbols that have been evaluated

$\endgroup$
3
  • $\begingroup$ This is a cool thing to know, but it doesn't really solve my problem. I really want the arguments included. The reason I'm doing this is so I can remember what arguments all my functions take. $\endgroup$
    – nullUser
    Nov 19, 2013 at 19:28
  • 1
    $\begingroup$ @nullUser You probably might be interested in defining templates for your functions: Multiple templates for a single user-defined function $\endgroup$
    – rm -rf
    Nov 19, 2013 at 20:05
  • $\begingroup$ But as I said in my answer, if you click on the function name you will see all the definitions you have made including all the argument patterns it accepts. $\endgroup$
    – m_goldberg
    Nov 19, 2013 at 21:09
0
$\begingroup$

How's your pattern matching? Mine's not at all good (yet), but you might be able to rustle up some kind of solution using this as a starting point:

functionScan[p_] := 
 StringCases[p, 
  result : ( 
     WordCharacter .. ~~ "[" ~~  Except[ "]", _] .. ~~ "]" ~~  _ ~~  
      ":=" ) :> result]

files = FileNames["GraphicalLSystems*.nb"];
result = functionScan[Import[#, "Plaintext"]] &  /@ files

with the result:

{{"LSystem[0,  axiom_,  _] :=", 
  "LSystem[iterations_,  axiom_,  rule_] :=", 
  "LOGO[commands_,  {startx_,  starty_,  starta_}] :=", 
  "geometry[system_,  iterations_] :=", "thumbnail[system_] :=", 
  "Module[{gosper,  abs,  arg}, gosper[abs_,  arg_,  baseAngle_] :="}, 
  {}, 
  {},
  {}}

Formatting the results should be easier!

$\endgroup$

Your Answer

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

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