4

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?

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
nullUser
  • 457
  • 2
  • 11
  • 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. – Sjoerd C. de Vries Nov 19 '13 at 16:04
  • @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. – nullUser Nov 19 '13 at 19:31
  • There are conditions where those headers are needed, for instance in functions that mutually reference each other. Not so in Mathematica. – Sjoerd C. de Vries Nov 19 '13 at 21:47
  • @SjoerdC.deVries Prototypes are needed for mutually referencing functions. A separate header file is not. – nullUser Nov 20 '13 at 05:10

3 Answers3

5

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.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
4

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

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
0

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!

cormullion
  • 24,243
  • 4
  • 64
  • 133