Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
    
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 5:10
add comment

3 Answers

up vote 2 down vote accepted

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.

share|improve this answer
    
This is perfect for my needs! –  nullUser Nov 19 '13 at 20:31
add comment

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

share|improve this answer
    
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. –  nullUser Nov 19 '13 at 19:28
1  
@nullUser You probably might be interested in defining templates for your functions: Multiple templates for a single user-defined function –  rm -rf Nov 19 '13 at 20:05
    
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. –  m_goldberg Nov 19 '13 at 21:09
add comment

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!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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